Hirdetés

2024. május 5., vasárnap

Gyorskeresés

Hozzászólások

(#887) ArchElf


ArchElf
addikt

using System;
using System.Collections.Generic;
using System.IO;

namespace TestCon
{
class Program
{
static string PBIN = "prime.bin";
static long PLEN = 1024 * 1024 * 512;
static MemoryStream MPSTREAM;
static FileStream PSTREAM;
static byte[] B235DATA = new byte[] {
0x82, 0x28, 0x8a, 0xa0, 0x20,
0x8a, 0x22, 0x28, 0x88, 0xa2,
0x08, 0x0a, 0xa2, 0x28, 0x82 };
static string PBMD5 = "75b7c17fa77f8d12017cf69fca36c626";
static int B235LENGTH = 15;

static void Main(string[] args)
{
try
{
PSTREAM = File.Open(PBIN, FileMode.OpenOrCreate, FileAccess.ReadWrite);
if (!checkmd5())
{
build();
loadbase();
build2();
}
else
loadbase();
PSTREAM.Close();
startApp();
}
catch (FormatException)
{
Console.WriteLine("\n\nPozitiv egesz szamokat lehet csak megadni.");
endApp();
}
catch (Exception)
{
Console.WriteLine("\n\nHiba tortent.");
endApp();
}
}

private static bool checkmd5()
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
md5.Initialize();
PSTREAM.Position = 0;
byte[] bmd5 = md5.ComputeHash(PSTREAM);
PSTREAM.Position = 0;
string st = "";
foreach (byte b in bmd5)
st += b.ToString("x2");
return (st == PBMD5);
}

private static void build()
{
long flength = PLEN / B235LENGTH;
for (long ix = 0; ix < flength; ix++)
PSTREAM.Write(B235DATA, 0, B235LENGTH);
PSTREAM.Write(B235DATA, 0, ((int)PLEN % B235LENGTH));
}

private static void loadbase()
{
MPSTREAM = new MemoryStream();
MPSTREAM.Capacity = 512 * 1024 * 1024;
byte[] bl = new byte[1024];
PSTREAM.Position = 0;
MPSTREAM.Position = 0;
for (int ix = 0; ix < 512 * 1024; ix++)
{
PSTREAM.Read(bl, 0, 1024);
MPSTREAM.Write(bl, 0, 1024);
}
MPSTREAM.Position = 0;
}

private static void build2()
{
// first byte mark
// 10101100 : 0xac
MPSTREAM.Seek(0, SeekOrigin.Begin);
MPSTREAM.WriteByte((byte)0xac);
int pbx, pby;
for (int ix = 7; ix < UInt16.MaxValue; ix++)
{
if ((pbx = GetPBit(ix)) == 1)
{
for (long iy = ix; iy < UInt32.MaxValue; iy++)
{
if ((pby=GetPBit(iy))==1)
{
long ip = (((long)ix) * iy);
if (ip <= ((long)UInt32.MaxValue))
ClearPBit(ip);
else
break;
}
else if (pby == -1)
iy = (((iy >> 3) + 1) << 3) - 1;
}
}
else if (pbx == -1)
ix = (((ix >> 1) + 1) << 1) - 1;

}
PSTREAM.Position = 0;
MPSTREAM.WriteTo(PSTREAM);
}

private static int GetPBit(long ix)
{
if (ix > UInt32.MaxValue) return -2;
int px = (int)(ix >> 3);
int pp = (int)(ix & 0x07);
MPSTREAM.Seek(px, SeekOrigin.Begin);
byte pb = (byte)MPSTREAM.ReadByte();
MPSTREAM.Seek(-1, SeekOrigin.Current);
if (pb == 0) return -1;
return ((pb >> pp) & 0x01);
}

private static void ClearPBit(long ix)
{
int px = (int)(ix >> 3);
int pp = (int)(ix & 0x07);
MPSTREAM.Seek(px, SeekOrigin.Begin);
byte pb = (byte)MPSTREAM.ReadByte();
MPSTREAM.Seek(-1, SeekOrigin.Current);
byte pm = (byte)((0x01 << pp) ^ 0xff);
pb = (byte)(pb & pm);
MPSTREAM.WriteByte(pb);
}

private static void endApp()
{
Console.WriteLine("\n\nA kilepeshez nyomjon le egy billentyut!");
Console.ReadKey();
}

private static void startApp()
{
Int64 szam;
Console.Write("Primszamvizsgalat. Kerem a vizsgalando szamot: ");
szam = Int64.Parse(Console.ReadLine());
if (szam > 1)
{
Console.WriteLine("\nPrimszamitas (új) folyamatban...\n");
if (isPrimeWithCountNew(szam))
Console.WriteLine("Primszam.");
else
Console.WriteLine("Nem primszam.");
}
else
{
Console.WriteLine("\n\nA vizsgalatot csak egynel nagyobb pozitiv egesz szamokra lehet elvegezni!");
}
endApp();
}

private static bool isPrimeWithCountNew(Int64 szam)
{
Console.WriteLine("Osztoi:");
List<long> osztok = new List<long>();
int result_x;
if ((result_x=GetPBit(szam)) != 1)
{
long bc = szam;
Int64 ix = 2;
while (true)
{
if (GetPBit(bc) == 1)
{
osztok.Add(bc);
break;
}
long sq = (long)Math.Floor(Math.Sqrt(bc));
if ((sq * sq == bc) && (GetPBit(sq) == 1))
{
osztok.Add(sq);
osztok.Add(sq);
break;
}
bool mul = false;
while (ix <= sq)
{
int result;
if ((result = GetPBit(ix)) == 1)
{
if (bc % ix == 0)
{
bc /= ix;
mul = true;
osztok.Add(ix);
break;
}
}
if (result == -1)
ix = (((ix >> 3) + 1) << 3);
else
ix++;
}
if (!mul)
{
if (bc != szam)
osztok.Add(bc);
break;
}
}
}
if (osztok.Count == 0)
{
Console.WriteLine("nincsenek");
return true;
}
else
{
Console.WriteLine("{0} prímtényezőre osztható, melyek a következők:", osztok.Count);
foreach (long l in osztok)
Console.Write("{0} ", l);
Console.WriteLine();
return false;
}
}
}
}

Csinálok egy adag popcornt, és leülök fórumozni --- Ízlések és pofonok - kinek miből jutott --- Az igazi beköpőlégy [http://is.gd/cJvlC2]

Copyright © 2000-2024 PROHARDVER Informatikai Kft.