79 lines
1.7 KiB
C#
79 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Program
|
|
{
|
|
class Sorozat
|
|
{
|
|
static Dictionary<long, int> memo = new Dictionary<long, int>();
|
|
|
|
static int CollatzLength(long n) {
|
|
|
|
long original = n;
|
|
int count = 0;
|
|
|
|
while (n != 1 && !memo.ContainsKey(n))
|
|
{
|
|
if (n % 2 == 0)
|
|
{
|
|
n /= 2;
|
|
} else
|
|
{
|
|
n = n * 3 + 1;
|
|
}
|
|
count++;
|
|
}
|
|
|
|
if (memo.ContainsKey(n))
|
|
{
|
|
count += memo[n];
|
|
}
|
|
|
|
memo[original] = count;
|
|
return count;
|
|
}
|
|
|
|
public static void CollatzLengthWithPrint(long n)
|
|
{
|
|
|
|
long original = n;
|
|
int count = 0;
|
|
|
|
while (n != 1)
|
|
{
|
|
if (n % 2 == 0)
|
|
{
|
|
n /= 2;
|
|
}
|
|
else
|
|
{
|
|
n = n * 3 + 1;
|
|
}
|
|
count++;
|
|
Console.WriteLine(n);
|
|
}
|
|
}
|
|
|
|
public static void ComputeCollatz(int limit)
|
|
{
|
|
int maxLenght = 0;
|
|
int maxNumber = 0;
|
|
|
|
for (int i = 1; i < limit; i++)
|
|
{
|
|
int length = Sorozat.CollatzLength(i);
|
|
if (length > maxLenght)
|
|
{
|
|
maxLenght = length;
|
|
maxNumber = i;
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"2. feladat megoldás: {maxNumber}|{maxLenght}");
|
|
}
|
|
}
|
|
}
|