Advertisement
huutho_96

MMH

May 4th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MMH
  8. {
  9.     class Prime
  10.     {
  11.         private int[] _primes;
  12.         private int _total = 0;
  13.  
  14.         public Prime()
  15.         {
  16.             _primes = new int[168];
  17.             init();
  18.         }
  19.         private bool temp_init(int n)
  20.         {
  21.             if (n < 2)
  22.                 return false;
  23.             if (n == 2)
  24.                 return true;
  25.  
  26.             int temp = (int)Math.Sqrt(n);
  27.  
  28.             int i = 0;
  29.             for (; i < _total && _primes[i] <= temp; i += 1)
  30.             {
  31.                 if (n % _primes[i] == 0)
  32.                     return false;
  33.             }
  34.  
  35.             return true;
  36.         }
  37.         private void init()
  38.         {
  39.             _primes[_total++] = 2;
  40.             for (int i = 3; i < 1000; i += 2)
  41.             {
  42.                 if (temp_init(i))
  43.                 {
  44.                     _primes[_total++] = i;
  45.                 }
  46.             }
  47.         }
  48.         public bool is_prime(int n)
  49.         {
  50.             if (n < 2)
  51.                 return false;
  52.  
  53.             if (n < _primes[_total - 1])
  54.             {
  55.                 int first = 0, last = _total -1;
  56.                 while (first <= last)
  57.                 {
  58.                     int mid = (first + last) / 2;
  59.                     if (_primes[mid] == n)
  60.                         return true;
  61.                     else
  62.                     {
  63.                         if (_primes[mid] < n)
  64.                         {
  65.                             first = mid + 1;
  66.                         }
  67.                         else
  68.                         {
  69.                             last = mid - 1;
  70.                         }
  71.                     }
  72.                 }
  73.                 return false;
  74.             }
  75.             else
  76.             {
  77.                 return temp_init(n);
  78.             }
  79.         }
  80.  
  81.         public int rand_prime(int min = 2, int max = 10000)
  82.         {
  83.             Random rand = new Random();
  84.             while (true)
  85.             {
  86.                 int n = rand.Next(min, max);
  87.                 if (is_prime(n))
  88.                     return n;
  89.             }
  90.             return -1;
  91.         }
  92.         public int max_prime(int max = 10000)
  93.         {
  94.             for (int i = max; i >= 2; i--)
  95.             {
  96.                 if (is_prime(i))
  97.                     return i;
  98.             }
  99.  
  100.             return -1;
  101.         }
  102.     }
  103.  
  104.     class GCD
  105.     {
  106.         public static int FindGCD(int a, int b)
  107.         {
  108.             while (a != b)
  109.             {
  110.                 if (a > b) a = a - b;
  111.                 else b = b - a;
  112.             }
  113.             return a;
  114.         }
  115.     }
  116.  
  117.     class Modulo
  118.     {
  119.         // x^e % n
  120.         public static int MOD (int x, int e, int n)
  121.         {
  122.             if (e == 1)
  123.                 return x % n;
  124.  
  125.             return ((x % n) * MOD(x, e - 1, n)) % n;
  126.         }
  127.     }
  128.     class Program
  129.     {
  130.         static void Main(string[] args)
  131.         {
  132.             Prime p = new Prime();
  133.             // random 1 so nguyen to trong khoang 2 den 10000
  134.             Console.WriteLine("So nguyen to ngau nhien n < 10000: n = " + p.rand_prime(2, 10000));
  135.  
  136.             // tim so nguyen to lon nhat nho hon 5000
  137.             Console.WriteLine("So nguyen to lon nhat n < 5000: n = " + p.max_prime(5000));
  138.  
  139.             // tim so nguyen to lon nhat nho hon 10000
  140.             Console.WriteLine("So nguyen to lon nhat n < 10000: n = " + p.max_prime(10000));
  141.  
  142.             // kiem tra so nguyen bat ky nho hon 1000000 co phai so nguyen to khong
  143.             Random rand = new Random();
  144.             int n = rand.Next(0, 1000000);
  145.             if (p.is_prime(n))
  146.                 Console.WriteLine(n + " la so nguyen to");
  147.             else
  148.                 Console.WriteLine(n + " khong phai la so nguyen to");
  149.  
  150.             // xac dinh uoc chung lon nhat cua 2 so duoi 10000
  151.             Console.WriteLine("UCLN(9892, 12) = " + GCD.FindGCD(9892, 12));
  152.  
  153.             // xac dinh mod cua x^e mod n
  154.             Console.WriteLine("(123 ^ 892) % 14 = " + Modulo.MOD(123, 892, 14));
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement