Advertisement
Guest User

Untitled

a guest
Mar 8th, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. // Find the largest Prime factor of 600851475143
  2. using System;
  3.  
  4. namespace ConsoleApplication1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Declare Vars
  11.             long primeFactorNumber = 600851475143;
  12.             int finalNumber = 0;
  13.  
  14.             // Use variable i for calculations until reasonable estimated number, i should equal 2 to save cycles
  15.             for (int i = 2; i < 100000; i++)
  16.             {
  17.                 if (primeFactorNumber % i == 0)
  18.                 {
  19.                     // Set instanced vars that I can work with
  20.                     // numberToWorkWith should equal 2 to save cycles
  21.                     int numberToWorkWith = 2;
  22.                     bool notAPrime = false;
  23.  
  24.                     // As long as numberToWorkWith is less than 10k (reasonable limit for this exercise), and notAPrime isn't trigged
  25.                     // Loop
  26.                     while (numberToWorkWith < 10000 && !notAPrime)
  27.                     {
  28.                         // If i is divisble by the working number (numberToWorkWith), it's not a prime number---flag
  29.                         if (i % numberToWorkWith == 0 && i != numberToWorkWith && i != 0)
  30.                         {
  31.                             notAPrime = true;
  32.                         }
  33.  
  34.                         // Increment to further loop
  35.                         numberToWorkWith++;
  36.                     }
  37.  
  38.                     // If the conditions are met, then it's a prime number
  39.                     if (!notAPrime)
  40.                     {
  41.                         finalNumber = i;
  42.                     }
  43.                 }
  44.             }
  45.             Console.WriteLine("Finished.  Your final prime number is {0}", finalNumber);
  46.             // Once finished, ensure program doesn't exit
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement