solution found for feladat 2

This commit is contained in:
Digi
2025-03-12 16:40:15 +01:00
parent c83b7eacbd
commit 490efd2fcc
17 changed files with 85 additions and 81 deletions

View File

@@ -8,64 +8,71 @@ namespace Program
{
class Sorozat
{
public int KezdoErtek;
static Dictionary<long, int> memo = new Dictionary<long, int>();
public List<int> Tagok = new List<int>();
static int CollatzLength(long n) {
int[] kettoHatvanyok = new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608 };
long original = n;
int count = 0;
public int TagokKiszamitasa(int ertek)
{
int ujertek;
if (kettoHatvanyok.Contains(ertek))
while (n != 1 && !memo.ContainsKey(n))
{
int index = 0;
while (kettoHatvanyok[index] != ertek)
if (n % 2 == 0)
{
index++;
n /= 2;
} else
{
n = n * 3 + 1;
}
KettoHatvanyokTagokHozzaAdasa(index);
return 1;
count++;
}
if (ertek == 1) {
Tagok.Add(ertek);
return 1;
}
if (ertek % 2 == 0)
if (memo.ContainsKey(n))
{
ujertek = ertek / 2;
} else
{
ujertek = ertek * 3 + 1;
count += memo[n];
}
Tagok.Add(ertek);
return TagokKiszamitasa(ujertek);
memo[original] = count;
return count;
}
public void KettoHatvanyokTagokHozzaAdasa(int index)
public static void CollatzLengthWithPrint(long n)
{
for (int i = index; i >= 0; i--)
long original = n;
int count = 0;
while (n != 1)
{
Tagok.Add(kettoHatvanyok[i]);
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = n * 3 + 1;
}
count++;
Console.WriteLine(n);
}
}
public Sorozat(Sorozat sorozat)
public static void ComputeCollatz(int limit)
{
KezdoErtek = sorozat.KezdoErtek;
Tagok = sorozat.Tagok;
}
int maxLenght = 0;
int maxNumber = 0;
public Sorozat(int kezdoErtek)
{
this.KezdoErtek = kezdoErtek;
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}");
}
}
}