Advertisement
AlFas

Mersenne Prime Research code

Oct 7th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace PrimeShits
  13. {
  14.     public partial class MainForm : Form
  15.     {
  16.         int primesNo = 100;
  17.  
  18.         public MainForm()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  24.         {
  25.             primesNo = (int)numericUpDown1.Value;
  26.         }
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.             int[] primes = FindPrimes(primesNo);
  30.             string[] results = new string[primesNo];
  31.             string[] periods = new string[primesNo];
  32.             DividePrimes(results, periods, ref primes);
  33.             string[] fileData = new string[primesNo];
  34.             string lastPrimeString = primes[primesNo - 1].ToString();
  35.             char[] lastPrimeLength = lastPrimeString.ToCharArray();
  36.             for (int i = 0; i < primesNo; i++)
  37.             {
  38.                 string iString = primes[i].ToString();
  39.                 char[] iLength = iString.ToCharArray();
  40.                 fileData[i] += "1 / ";
  41.                 for (int j = 1; j < lastPrimeLength.Length - iLength.Length + 1; j++)
  42.                     fileData[i] += "0";
  43.                 fileData[i] += primes[i].ToString() + ": Period grade: ";
  44.                 fileData[i] += periods[i].ToString();
  45.             }
  46.             File.WriteAllLines("C:/users/user/Desktop/primes.txt", fileData);
  47.         }
  48.         void DividePrimes(string[] results, string[] periods, ref int[] primes)
  49.         {
  50.             for (int i = 0; i < primesNo; i++)
  51.             {
  52.                 bool period = false;
  53.                 int remainder = 10;
  54.                 string result = "0.";
  55.                 int times = 0;
  56.                 while (period == false)
  57.                 {
  58.                     while (remainder >= primes[i])
  59.                     {
  60.                         remainder -= primes[i];
  61.                         times++;
  62.                     }
  63.                     result += times.ToString();
  64.                     times = 0;
  65.                     remainder *= 10;
  66.                     if (remainder == 10)
  67.                         period = true;
  68.                     if (remainder == 0)
  69.                         break;
  70.                 }
  71.                 results[i] = result;
  72.                 char[] resultChar = result.ToCharArray();
  73.                 int periodLength = resultChar.Length - 2;
  74.                 periods[i] = periodLength.ToString();
  75.             }
  76.         }
  77.         int[] FindPrimes(int totalPrimes)
  78.         {
  79.             int primesFound = 0;
  80.             int no = 2;
  81.             int[] primes = new int[totalPrimes];
  82.             while (primesFound < totalPrimes)
  83.             {
  84.                 bool isPrime = false;
  85.                 int totalNumbers = 0;
  86.                 for (int i = 1; i <= no; i++)
  87.                 {
  88.                     int intResult = no / i;
  89.                     double doubleResult = (double)no / (double)i;
  90.                     if ((double)intResult == doubleResult)
  91.                         totalNumbers++;
  92.                     if (totalNumbers > 2)
  93.                         break;
  94.                 }
  95.                 if (totalNumbers == 2)
  96.                     isPrime = true;
  97.                 if (isPrime == true)
  98.                 {
  99.                     primes[primesFound] = no;
  100.                     primesFound++;
  101.                 }
  102.                 totalNumbers = 0;
  103.                 isPrime = false;
  104.                 no++;
  105.             }
  106.             return primes;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement