Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace PrimeShits
- {
- public partial class MainForm : Form
- {
- int primesNo = 100;
- public MainForm()
- {
- InitializeComponent();
- }
- private void numericUpDown1_ValueChanged(object sender, EventArgs e)
- {
- primesNo = (int)numericUpDown1.Value;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int[] primes = FindPrimes(primesNo);
- string[] results = new string[primesNo];
- string[] periods = new string[primesNo];
- DividePrimes(results, periods, ref primes);
- string[] fileData = new string[primesNo];
- string lastPrimeString = primes[primesNo - 1].ToString();
- char[] lastPrimeLength = lastPrimeString.ToCharArray();
- for (int i = 0; i < primesNo; i++)
- {
- string iString = primes[i].ToString();
- char[] iLength = iString.ToCharArray();
- fileData[i] += "1 / ";
- for (int j = 1; j < lastPrimeLength.Length - iLength.Length + 1; j++)
- fileData[i] += "0";
- fileData[i] += primes[i].ToString() + ": Period grade: ";
- fileData[i] += periods[i].ToString();
- }
- File.WriteAllLines("C:/users/user/Desktop/primes.txt", fileData);
- }
- void DividePrimes(string[] results, string[] periods, ref int[] primes)
- {
- for (int i = 0; i < primesNo; i++)
- {
- bool period = false;
- int remainder = 10;
- string result = "0.";
- int times = 0;
- while (period == false)
- {
- while (remainder >= primes[i])
- {
- remainder -= primes[i];
- times++;
- }
- result += times.ToString();
- times = 0;
- remainder *= 10;
- if (remainder == 10)
- period = true;
- if (remainder == 0)
- break;
- }
- results[i] = result;
- char[] resultChar = result.ToCharArray();
- int periodLength = resultChar.Length - 2;
- periods[i] = periodLength.ToString();
- }
- }
- int[] FindPrimes(int totalPrimes)
- {
- int primesFound = 0;
- int no = 2;
- int[] primes = new int[totalPrimes];
- while (primesFound < totalPrimes)
- {
- bool isPrime = false;
- int totalNumbers = 0;
- for (int i = 1; i <= no; i++)
- {
- int intResult = no / i;
- double doubleResult = (double)no / (double)i;
- if ((double)intResult == doubleResult)
- totalNumbers++;
- if (totalNumbers > 2)
- break;
- }
- if (totalNumbers == 2)
- isPrime = true;
- if (isPrime == true)
- {
- primes[primesFound] = no;
- primesFound++;
- }
- totalNumbers = 0;
- isPrime = false;
- no++;
- }
- return primes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement