Created
February 7, 2014 12:14
-
-
Save EslaMx7/8861606 to your computer and use it in GitHub Desktop.
Project Euler Problem 3 Solution with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace ConsolesTests | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
long NUM = 600851475143; | |
long LargestPrime = 0; | |
// NUM = (long)Math.Sqrt(NUM); | |
for (long i = 2; i*i < NUM; i++) | |
{ | |
if (IsFactorOf(NUM, i)) | |
if (IsPrime(i)) | |
{ | |
Console.WriteLine(i + " : is prime & is factor "); | |
if (i > LargestPrime) | |
LargestPrime = i; | |
} | |
} | |
Console.WriteLine("\n Done.... and the Largest Prime Factor is : " +LargestPrime); | |
} | |
static bool IsPrime(long x) { | |
for (int i = 2; i < x;++i ) | |
if (x % i == 0) | |
return false; | |
return true; | |
} | |
static bool IsFactorOf(long x, long y) | |
{ | |
if (x % y == 0) | |
return true; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh, i think this solution will take tooo much time doesn't it ? i tried this before and solution took about half an our on a code2due processor if i remember.