Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 10th, 2012  |  syntax: None  |  size: 1.80 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace ConsultaNumerosPrimos
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.Write("Deseja verificar qual numero?(0-10^7):");
  15.             long i = long.Parse(Console.ReadLine());
  16.             string subject = Directory.GetCurrentDirectory() + "\\data.bin";
  17.             FileStream fs = new FileStream(subject, FileMode.Open);
  18.             long pos = (i - i % 8) / 8;
  19.             fs.Seek(pos, SeekOrigin.Begin);
  20.             byte b =(byte)fs.ReadByte();
  21.             char c = ToBinary(b).PadRight(8,'0').ToCharArray()[i % 8];
  22.             Console.WriteLine(i.ToString()+(c=='1'?" é primo!":"não é primo!"));
  23.             Console.ReadLine();
  24.         }
  25.         public static string ReverseString(string s)
  26.         {
  27.             char[] arr = s.ToCharArray();
  28.             Array.Reverse(arr);
  29.             return new string(arr);
  30.         }
  31.         public static string ToBinary(Int64 Decimal)
  32.         {
  33.             // Declare a few variables we're going to need
  34.             Int64 BinaryHolder;
  35.             char[] BinaryArray;
  36.             string BinaryResult = "";
  37.  
  38.             while (Decimal > 0)
  39.             {
  40.                 BinaryHolder = Decimal % 2;
  41.                 BinaryResult += BinaryHolder;
  42.                 Decimal = Decimal / 2;
  43.             }
  44.  
  45.             // The algorithm gives us the binary number in reverse order (mirrored)
  46.             // We store it in an array so that we can reverse it back to normal
  47.             BinaryArray = BinaryResult.ToCharArray();
  48.             Array.Reverse(BinaryArray);
  49.             BinaryResult = new string(BinaryArray);
  50.  
  51.             return BinaryResult;
  52.         }
  53.     }
  54. }