public void TestCollatzChainLengthOf() { var answer = new Collatz().CollatzChainLengthOf(13); var expected = 10; Assert.AreEqual(expected, answer); }
public static void collatz_count_test( ) //****************************************************************************80 // // Purpose: // // COLLATZ_COUNT_TEST tests COLLATZ_COUNT. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 March 2006 // // Author: // // John Burkardt // { int count = 0; int n = 0; Console.WriteLine(""); Console.WriteLine("COLLATZ_COUNT_TEST:"); Console.WriteLine(" COLLATZ_COUNT(N) counts the length of the"); Console.WriteLine(" Collatz sequence beginning with N."); Console.WriteLine(""); Console.WriteLine(" N COUNT(N) COUNT(N)"); Console.WriteLine(" (computed) (table)"); Console.WriteLine(""); int n_data = 0; for ( ; ;) { Burkardt.Values.Collatz.collatz_count_values(ref n_data, ref n, ref count); if (n_data == 0) { break; } int count2 = Collatz.collatz_count(n); Console.WriteLine(" " + n.ToString().PadLeft(8) + " " + count.ToString().PadLeft(8) + " " + count2.ToString().PadLeft(8) + ""); } }
internal static long Run() { // number, chain length Tuple <long, long> currentLongest = new Tuple <long, long>(1, 1); for (int i = 1; i < 1000000; i++) { long length = Collatz.CollatzSequenceChainLength(i); if (length > currentLongest.Item2) { currentLongest = new Tuple <long, long>(i, length); } } return(currentLongest.Item1); }
public static void collatz_count_max_test( ) //****************************************************************************80 // // Purpose: // // COLLATZ_COUNT_MAX_TEST tests COLLATZ_COUNT_MAX. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 April 2009 // // Author: // // John Burkardt // { int i_max = 0; int j_max = 0; Console.WriteLine(""); Console.WriteLine("COLLATZ_COUNT_MAX_TEST:"); Console.WriteLine(" COLLATZ_COUNT_MAX(N) returns the length of the"); Console.WriteLine(" longest Collatz sequence from 1 to N."); Console.WriteLine(""); Console.WriteLine(" N I_MAX J_MAX"); Console.WriteLine(""); int n = 10; while (n <= 100000) { Collatz.collatz_count_max(n, ref i_max, ref j_max); Console.WriteLine(" " + n.ToString().PadLeft(8) + " " + i_max.ToString().PadLeft(8) + " " + j_max.ToString().PadLeft(8) + ""); n *= 10; } }
public static void collatz_count_values_test() //****************************************************************************80 // // Purpose: // // COLLATZ_COUNT_VALUES_TEST tests COLLATZ_COUNT_VALUES. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 07 March 2006 // // Author: // // John Burkardt // { int count = 0; int n = 0; Console.WriteLine(""); Console.WriteLine("COLLATZ_COUNT_VALUES_TEST:"); Console.WriteLine(" COLLATZ_COUNT_VALUES returns values of"); Console.WriteLine(" the length of the Collatz sequence that"); Console.WriteLine(" starts at N."); Console.WriteLine(""); Console.WriteLine(" N COLLATZ_COUNT(N)"); Console.WriteLine(""); int n_data = 0; for (;;) { Collatz.collatz_count_values(ref n_data, ref n, ref count); if (n_data == 0) { break; } Console.WriteLine(" " + n.ToString().PadLeft(8) + " " + count.ToString().PadLeft(12) + ""); } }
public void CollatzSequenceTest() { IEnumerable <long> list = Collatz.CollatzSequence(13); // 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 Assert.AreEqual(13, list.ElementAt(0)); Assert.AreEqual(40, list.ElementAt(1)); Assert.AreEqual(20, list.ElementAt(2)); Assert.AreEqual(10, list.ElementAt(3)); Assert.AreEqual(5, list.ElementAt(4)); Assert.AreEqual(16, list.ElementAt(5)); Assert.AreEqual(8, list.ElementAt(6)); Assert.AreEqual(4, list.ElementAt(7)); Assert.AreEqual(2, list.ElementAt(8)); Assert.AreEqual(1, list.ElementAt(9)); Assert.AreEqual(10, list.Count()); }
static void Main(string[] args) { var largestChain = 0; var collatz = new Collatz(); for (int i = 1; i < 1000000; i++) { var chainLength = collatz.CollatzChainLengthOf(i); if (largestChain < chainLength) { Console.WriteLine("{0} {1}", i, chainLength); largestChain = chainLength; } } Console.WriteLine("Finished"); Console.ReadKey(); }
public void CollatzIterative() { Assert.AreEqual(0, Collatz.collatz_iterative(1)); Assert.AreEqual(1, Collatz.collatz_iterative(2)); Assert.AreEqual(2, Collatz.collatz_iterative(4)); Assert.AreEqual(3, Collatz.collatz_iterative(8)); Assert.AreEqual(4, Collatz.collatz_iterative(16)); Assert.AreEqual(5, Collatz.collatz_iterative(5)); Assert.AreEqual(5, Collatz.collatz_iterative(32)); Assert.AreEqual(6, Collatz.collatz_iterative(10)); Assert.AreEqual(6, Collatz.collatz_iterative(64)); Assert.AreEqual(7, Collatz.collatz_iterative(3)); Assert.AreEqual(7, Collatz.collatz_iterative(20)); Assert.AreEqual(7, Collatz.collatz_iterative(21)); Assert.AreEqual(7, Collatz.collatz_iterative(128)); }
public (long startingNumber, long length) LongestCollatzsequence(int below) { var longestCollatzsequence = (startingNumber : 0, length : 0); for (int startingNumber = 1; startingNumber < below; startingNumber++) { var collatzsequenceCount = new Collatz().Iterator(startingNumber).Count(); if (collatzsequenceCount > longestCollatzsequence.length) { longestCollatzsequence = (startingNumber : startingNumber, length : collatzsequenceCount); } } return(longestCollatzsequence); }
public void testGrande() { Assert.Equal(152, Collatz.Passi(1000000)); }
public void CollatzRecursiveNegative() { Assert.AreEqual(-1, Collatz.collatz_recursive(0)); Assert.AreEqual(-1, Collatz.collatz_recursive(-1)); Assert.AreEqual(-1, Collatz.collatz_recursive(-5)); }
public void CollatzIterativeNegative() { Assert.AreEqual(-1, Collatz.collatz_iterative(0)); Assert.AreEqual(-1, Collatz.collatz_iterative(-1)); Assert.AreEqual(-1, Collatz.collatz_iterative(-5)); }
public void test1() { Assert.Equal(0, Collatz.Passi(1)); }
public void testNegativo() { Assert.Throws <ArgumentException>(() => Collatz.Passi(-15)); }
public void testZero() { Assert.Throws <ArgumentException>(() => Collatz.Passi(0)); }
public void test12() { Assert.Equal(9, Collatz.Passi(12)); }
public void test16() { Assert.Equal(4, Collatz.Passi(16)); }