
Untitled
By: a guest on
Jul 10th, 2012 | syntax:
None | size: 1.80 KB | hits: 13 | expires: Never
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsultaNumerosPrimos
{
class Program
{
static void Main(string[] args)
{
Console.Write("Deseja verificar qual numero?(0-10^7):");
long i = long.Parse(Console.ReadLine());
string subject = Directory.GetCurrentDirectory() + "\\data.bin";
FileStream fs = new FileStream(subject, FileMode.Open);
long pos = (i - i % 8) / 8;
fs.Seek(pos, SeekOrigin.Begin);
byte b =(byte)fs.ReadByte();
char c = ToBinary(b).PadRight(8,'0').ToCharArray()[i % 8];
Console.WriteLine(i.ToString()+(c=='1'?" é primo!":"não é primo!"));
Console.ReadLine();
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
public static string ToBinary(Int64 Decimal)
{
// Declare a few variables we're going to need
Int64 BinaryHolder;
char[] BinaryArray;
string BinaryResult = "";
while (Decimal > 0)
{
BinaryHolder = Decimal % 2;
BinaryResult += BinaryHolder;
Decimal = Decimal / 2;
}
// The algorithm gives us the binary number in reverse order (mirrored)
// We store it in an array so that we can reverse it back to normal
BinaryArray = BinaryResult.ToCharArray();
Array.Reverse(BinaryArray);
BinaryResult = new string(BinaryArray);
return BinaryResult;
}
}
}