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.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System.IO;
- namespace PrimeR
- {
- public partial class MainForm : Form
- {
- int primesNo = 100;
- int totalProgress, stage1progress, stage2progress, stage3progress, stage4progress, stage5progress;
- int stage1progressMax, stage2progressMax, stage3progressMax, stage4progressMax, stage5progressMax = 1;
- IntPtr thisProcess = new IntPtr(Process.GetCurrentProcess().Id);
- BackgroundWorker calculate = new BackgroundWorker();
- bool doneCalculating = true;
- public MainForm()
- {
- InitializeComponent();
- calculate.DoWork += Calculate;
- calculate.WorkerSupportsCancellation = true;
- TaskbarProgress.SetState(thisProcess, TaskbarProgress.TaskbarStates.Paused);
- }
- private void numericUpDown1_ValueChanged(object sender, EventArgs e)
- {
- primesNo = (int)numericUpDown1.Value;
- }
- private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- decimal value = Convert.ToDecimal(numericUpDown1.Text);
- try { numericUpDown1.Value = value; }
- catch (ArgumentOutOfRangeException)
- {
- if (value > numericUpDown1.Maximum) numericUpDown1.Value = numericUpDown1.Maximum;
- else if (value < numericUpDown1.Minimum) numericUpDown1.Value = numericUpDown1.Minimum;
- }
- button1_Click(sender, e);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- doneCalculating = false;
- calculate.RunWorkerAsync();
- progressBar1.Value = progressBar2.Value = progressBar3.Value = progressBar4.Value = progressBar5.Value = progressBar6.Value = 0;
- stage1progressMax = stage2progressMax = stage3progressMax = stage4progressMax = primesNo;
- while (!doneCalculating)
- {
- UpdateControls();
- UpdateControlValues();
- }
- UpdateControls();
- UpdateControlValues();
- totalProgress = stage1progress = stage2progress = stage3progress = stage4progress = stage5progress = 0;
- }
- public void Calculate(object sender, DoWorkEventArgs e)
- {
- int[] primes = FindPrimes(primesNo);
- string[] periods = new string[primesNo];
- string[] relations = new string[primesNo];
- string[] fileData = new string[primesNo];
- DividePrimes(periods, ref primes);
- FindRelation(periods, relations, ref primes);
- FindPrimeRelations(ref primes, ref periods, ref fileData, ref relations);
- FindDenominators(ref primes, ref relations);
- doneCalculating = true;
- }
- void UpdateControls()
- {
- progressBar1.Update();
- progressBar2.Update();
- progressBar3.Update();
- progressBar4.Update();
- progressBar5.Update();
- progressBar6.Update();
- label4.Update();
- label5.Update();
- label6.Update();
- label7.Update();
- label8.Update();
- label10.Update();
- groupBox1.Update();
- }
- void UpdateControlValues()
- {
- progressBar1.Value = totalProgress;
- progressBar2.Value = (int)Math.Round(((double)stage1progress / (double)stage1progressMax) * 1000);
- progressBar3.Value = (int)Math.Round(((double)stage2progress / (double)stage2progressMax) * 1000);
- progressBar4.Value = (int)Math.Round(((double)stage3progress / (double)stage3progressMax) * 1000);
- progressBar5.Value = (int)Math.Round(((double)stage4progress / (double)stage4progressMax) * 1000);
- progressBar6.Value = (int)Math.Round(((double)stage5progress / (double)stage5progressMax) * 1000);
- label4.Text = GetPercentageString(progressBar2.Value);
- label5.Text = GetPercentageString(progressBar3.Value);
- label6.Text = GetPercentageString(progressBar6.Value);
- label7.Text = GetPercentageString(totalProgress);
- label8.Text = GetPercentageString(progressBar4.Value);
- label10.Text = GetPercentageString(progressBar5.Value);
- TaskbarProgress.SetValue(thisProcess, totalProgress, 1000);
- }
- string GetPercentageString(int percent)
- {
- double percentage = (double)percent / 10;
- string percentageString = percentage.ToString() + "%";
- if (percent / 10 == percentage)
- percentageString = percentage.ToString() + ".0%";
- return percentageString;
- }
- void FindPrimeRelations(ref int[] primes, ref string[] periods, ref string[] fileData, ref string[] relations)
- {
- char[] lastPrimeLength = primes[primesNo - 1].ToString().ToCharArray();
- char[] biggestPeriodLength = periods.ToInt32Array().Max().ToString().ToCharArray();
- for (int i = 0; i < primesNo; i++)
- {
- string iString = primes[i].ToString();
- char[] iLength = iString.ToCharArray();
- char[] periodLength = periods[i].ToString().ToCharArray();
- fileData[i] += "1 / ";
- for (int j = 1; j < lastPrimeLength.Length - iLength.Length + 1; j++)
- fileData[i] += " ";
- fileData[i] += primes[i].ToString() + " | Period grade: ";
- for (int j = 1; j < biggestPeriodLength.Length - periodLength.Length + 1; j++)
- fileData[i] += " ";
- fileData[i] += periods[i];
- if (Convert.ToInt32(relations[i]) == 0)
- fileData[i] += "\t | No relation.";
- else if (Convert.ToInt32(relations[i]) == 1)
- fileData[i] += "\t | k - 1";
- else
- fileData[i] += "\t | (k - 1) / " + relations[i];
- Progress(ref stage4progress, primesNo);
- }
- File.WriteAllLines("Primes - " + primesNo.ToString() + ".txt", fileData);
- }
- void FindDenominators(ref int[] primes, ref string[] relations)
- {
- int[] denominator = new int[primes[primesNo - 1]];
- string[] denominators = new string[primes[primesNo - 1]];
- stage5progressMax = denominators.Length;
- for (int i = 0; i < denominator.Length; i++)
- denominators[i] = "None";
- for (int i = 0; i < primes.Length; i++)
- {
- denominator[Convert.ToInt32(relations[i])]++;
- denominators[Convert.ToInt32(relations[i])] = denominator[Convert.ToInt32(relations[i])].ToString();
- }
- char[] mostFrequentDenominatorLength = denominators[1].ToString().ToCharArray();
- string[] denominatorFileData = new string[denominators.Length];
- for (int i = 0; i < denominators.Length; i++)
- {
- char[] iLength = denominators[i].ToCharArray();
- char[] currentDenominatorLength = i.ToString().ToCharArray();
- char[] lastDenominatorLength = denominators.Length.ToString().ToCharArray();
- for (int j = 1; j < lastDenominatorLength.Length - currentDenominatorLength.Length + 1; j++)
- denominatorFileData[i] += " ";
- denominatorFileData[i] += i.ToString() + ":\t";
- for (int j = 1; j < Math.Max(mostFrequentDenominatorLength.Length, 4) - iLength.Length + 1; j++)
- denominatorFileData[i] += " ";
- denominatorFileData[i] += denominators[i] + " (" + (((double)denominator[i] / (double)primesNo) * 100).ToString() + "%)";
- Progress(ref stage5progress, denominators.Length);
- }
- File.WriteAllLines("Denominators - " + primesNo.ToString() + ".txt", denominatorFileData);
- }
- int[] FindPrimes(int totalPrimes)
- {
- // Make further optimizations
- int primesFound = 1;
- Progress(ref stage1progress, primesNo);
- int no = 3;
- int[] primes = new int[totalPrimes];
- primes[0] = 2;
- while (primesFound < totalPrimes)
- {
- bool isPrime = false;
- int totalNumbers = 0;
- for (int i = 1; i <= primes[primesFound - 1]; i++)
- {
- int intResult = no / i;
- double doubleResult = (double)no / (double)i;
- if ((double)intResult == doubleResult)
- totalNumbers++;
- if (totalNumbers > 1)
- break;
- }
- if (totalNumbers == 1)
- isPrime = true;
- if (isPrime)
- {
- primes[primesFound] = no;
- primesFound++;
- Progress(ref stage1progress, primesNo);
- }
- totalNumbers = 0;
- isPrime = false;
- no += 2;
- }
- return primes;
- }
- void DividePrimes(string[] periods, ref int[] primes)
- {
- for (int i = 0; i < primesNo; i++)
- {
- bool period = false;
- int remainder = 10;
- int periodLength = 0;
- while (period == false)
- {
- while (remainder >= primes[i])
- remainder -= primes[i];
- remainder *= 10;
- periodLength++;
- if (remainder == 10)
- period = true;
- if (remainder == 0)
- break;
- }
- if (remainder == 10)
- periods[i] = periodLength.ToString();
- else if (remainder == 0)
- periods[i] = "0";
- Progress(ref stage2progress, primesNo);
- }
- }
- void FindRelation(string[] periods, string[] relations, ref int[] primes)
- {
- for (int i = 0; i < primesNo; i++)
- {
- for (int j = 1; j <= primes[i]; j++)
- if ((Convert.ToInt32(periods[i]) * j) + 1 == primes[i])
- {
- relations[i] = j.ToString();
- break;
- }
- Progress(ref stage3progress, primesNo);
- }
- }
- private void Progress(ref int stage, int stageValue)
- {
- stage++;
- int rawStagePercentage = (int)Math.Round(((double)stage / (double)stageValue) * 1000);
- totalProgress = (int)Math.Round(((stage1progress / (decimal)stage1progressMax) + (stage2progress / (decimal)stage2progressMax) + (stage3progress / (decimal)stage3progressMax) + (stage4progress / (decimal)stage4progressMax) + (stage5progress / (decimal)stage5progressMax)) * 1000 / 5);
- }
- }
- public static class Extensions
- {
- public static int Max(this int[] ar)
- {
- int max = int.MinValue;
- for (int i = 0; i < ar.Length; i++)
- if (ar[i] > max)
- max = ar[i];
- return max;
- }
- public static int[] ToInt32Array(this string[] ar)
- {
- int[] result = new int[ar.Length];
- for (int i = 0; i < ar.Length; i++)
- result[i] = Convert.ToInt32(ar[i]);
- return result;
- }
- }
- public static class TaskbarProgress
- {
- public enum TaskbarStates
- {
- NoProgress = 0,
- Indeterminate = 0x1,
- Normal = 0x2,
- Error = 0x4,
- Paused = 0x8
- }
- [ComImportAttribute()]
- [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
- [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
- private interface ITaskbarList3
- {
- // ITaskbarList
- [PreserveSig]
- void HrInit();
- [PreserveSig]
- void AddTab(IntPtr hwnd);
- [PreserveSig]
- void DeleteTab(IntPtr hwnd);
- [PreserveSig]
- void ActivateTab(IntPtr hwnd);
- [PreserveSig]
- void SetActiveAlt(IntPtr hwnd);
- // ITaskbarList2
- [PreserveSig]
- void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
- // ITaskbarList3
- [PreserveSig]
- void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
- [PreserveSig]
- void SetProgressState(IntPtr hwnd, TaskbarStates state);
- }
- [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
- [ClassInterfaceAttribute(ClassInterfaceType.None)]
- [ComImportAttribute()]
- private class TaskbarInstance { }
- private static ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();
- private static bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);
- /// <summary>Used to change the TaskBarIcon's state.</summary>
- public static void SetState(IntPtr windowHandle, TaskbarStates taskbarState)
- {
- if (taskbarSupported) taskbarInstance.SetProgressState(windowHandle, taskbarState);
- }
- /// <summary>Used to change the TaskBarIcon's progress.</summary>
- public static void SetValue(IntPtr windowHandle, double progressValue, double progressMax)
- {
- if (taskbarSupported) taskbarInstance.SetProgressValue(windowHandle, (ulong)progressValue, (ulong)progressMax);
- }
- }
- public static class ISynchronizeInvokeExtensions
- {
- public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
- {
- if (@this.InvokeRequired)
- @this.Invoke(action, new object[] { @this });
- else
- action(@this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement