Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
44
Task/Hailstone-sequence/C-sharp/hailstone-sequence-1.cs
Normal file
44
Task/Hailstone-sequence/C-sharp/hailstone-sequence-1.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Hailstone
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static List<int> hs(int n,List<int> seq)
|
||||
{
|
||||
List<int> sequence = seq;
|
||||
sequence.Add(n);
|
||||
if (n == 1)
|
||||
{
|
||||
return sequence;
|
||||
}else{
|
||||
int newn = (n % 2 == 0) ? n / 2 : (3 * n) + 1;
|
||||
return hs(newn, sequence);
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int n = 27;
|
||||
List<int> sequence = hs(n,new List<int>());
|
||||
Console.WriteLine(sequence.Count + " Elements");
|
||||
List<int> start = sequence.GetRange(0, 4);
|
||||
List<int> end = sequence.GetRange(sequence.Count - 4, 4);
|
||||
Console.WriteLine("Starting with : " + string.Join(",", start) + " and ending with : " + string.Join(",", end));
|
||||
int number = 0, longest = 0;
|
||||
for (int i = 1; i < 100000; i++)
|
||||
{
|
||||
int count = (hs(i, new List<int>())).Count;
|
||||
if (count > longest)
|
||||
{
|
||||
longest = count;
|
||||
number = i;
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Number < 100000 with longest Hailstone seq.: " + number + " with length of " + longest);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Task/Hailstone-sequence/C-sharp/hailstone-sequence-2.cs
Normal file
42
Task/Hailstone-sequence/C-sharp/hailstone-sequence-2.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ConsoleApplication1
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
int longestChain = 0, longestNumber = 0;
|
||||
|
||||
var recursiveLengths = new Dictionary<int, int>();
|
||||
|
||||
const int maxNumber = 100000;
|
||||
|
||||
for (var i = 1; i <= maxNumber; i++)
|
||||
{
|
||||
var chainLength = Hailstone(i, recursiveLengths);
|
||||
if (longestChain >= chainLength)
|
||||
continue;
|
||||
|
||||
longestChain = chainLength;
|
||||
longestNumber = i;
|
||||
}
|
||||
Console.WriteLine("max below {0}: {1} ({2} steps)", maxNumber, longestNumber, longestChain);
|
||||
}
|
||||
|
||||
private static int Hailstone(int num, Dictionary<int, int> lengths)
|
||||
{
|
||||
if (num == 1)
|
||||
return 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (lengths.ContainsKey(num))
|
||||
return lengths[num];
|
||||
|
||||
lengths[num] = 1 + ((num%2 == 0) ? Hailstone(num/2, lengths) : Hailstone((3*num) + 1, lengths));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue