Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Find the largest Prime factor of 600851475143
- using System;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Declare Vars
- long primeFactorNumber = 600851475143;
- int finalNumber = 0;
- // Use variable i for calculations until reasonable estimated number, i should equal 2 to save cycles
- for (int i = 2; i < 100000; i++)
- {
- if (primeFactorNumber % i == 0)
- {
- // Set instanced vars that I can work with
- // numberToWorkWith should equal 2 to save cycles
- int numberToWorkWith = 2;
- bool notAPrime = false;
- // As long as numberToWorkWith is less than 10k (reasonable limit for this exercise), and notAPrime isn't trigged
- // Loop
- while (numberToWorkWith < 10000 && !notAPrime)
- {
- // If i is divisble by the working number (numberToWorkWith), it's not a prime number---flag
- if (i % numberToWorkWith == 0 && i != numberToWorkWith && i != 0)
- {
- notAPrime = true;
- }
- // Increment to further loop
- numberToWorkWith++;
- }
- // If the conditions are met, then it's a prime number
- if (!notAPrime)
- {
- finalNumber = i;
- }
- }
- }
- Console.WriteLine("Finished. Your final prime number is {0}", finalNumber);
- // Once finished, ensure program doesn't exit
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement