patterncsharpMinor
Project Euler #3 in C# - largest prime factor
Viewed 0 times
largestprojecteulerfactorprime
Problem
I just started doing the Project Euler challenges and right now I'm on Challenge #3 - The largest prime factor of 600851475143.
I wrote some code and tried to optimize it based on some other Stack Overflow users' advice.
What I got is this code which does its job, but I think it takes longer than it should - calculating high numbers like 600851475143 takes forever!
Am I in the right way? How could I optimize it?
I wrote some code and tried to optimize it based on some other Stack Overflow users' advice.
What I got is this code which does its job, but I think it takes longer than it should - calculating high numbers like 600851475143 takes forever!
Am I in the right way? How could I optimize it?
using System;
namespace ProjectEuler_LargestPrimeFactor
{
class Program
{
static long numberToFactor = 600851475143;
static void Main(string[] args)
{
Console.WriteLine(LargestPrimeFactorOf(numberToFactor) + " is the largest prime factor of " + numberToFactor);
Console.ReadKey();
}
static long LargestPrimeFactorOf(long n)
{
long lastPrimeFactor = 0;
for (long i = 2; i 2 && n % 2 == 0) || n == 1) return false;
for (long i = 2; i <= Math.Floor(Math.Sqrt(n)); ++i)
{
if (n % i == 0) return false;
}
return true;
}
}
}Solution
Here's my solution to this problem
First of all most of the exercises you will find on Project Euler will require some math formula or concept in order to achieve the highest possible performance. It's not pure programming and thus not the best place to practice it in my opinion (ofc assuming you don't know all the formulas) you will probably learn more math than programming in the process which is a good thing if that's what you are looking for. Long story short look for formula before you go into the programming.
Back to your actual code :
Why is your code working slow ?
There are few points here that I want to make first method calls are slower than just a bunch of code in a single method again some problems you will find later in Project Euler will be pretty easy to solve but hard to optimize so the usual C# code has everything separated in methods and classes because the performance loss is not that big in a normal project however here this might be your only bottleneck in some cases. You should always think in perspective what if you had even bigger number ? Would your method still work fast enough ? A working program doesn't mean that it's a good program.
Next you start thinking what can you improve and why exactly you are doing certain things. Do you really need to know every single number that is prime ?
In my solution there is simple prime factorization starting from the smallest prime we keep increasing the current number we have until we get a number that we can divide only by itself i.e the largest prime factor. 100,000 iterations run for about 3000-3200 ms on my machine.
static void Main(string[] args)
{
long num = 600851475143;
int count = 3;
Stopwatch sw = Stopwatch.StartNew();
while (num > 1)
{
if (num%count == 0)
{
num /= count;
}
else
{
count += 2;
}
}
sw.Stop();
Console.WriteLine("The largest prime factor of the number {0} is {1} ", 600851475143, count);
Console.WriteLine("Time to calculate in milliseconds : {0}", sw.ElapsedMilliseconds);
Console.ReadKey();
}First of all most of the exercises you will find on Project Euler will require some math formula or concept in order to achieve the highest possible performance. It's not pure programming and thus not the best place to practice it in my opinion (ofc assuming you don't know all the formulas) you will probably learn more math than programming in the process which is a good thing if that's what you are looking for. Long story short look for formula before you go into the programming.
Back to your actual code :
Why is your code working slow ?
There are few points here that I want to make first method calls are slower than just a bunch of code in a single method again some problems you will find later in Project Euler will be pretty easy to solve but hard to optimize so the usual C# code has everything separated in methods and classes because the performance loss is not that big in a normal project however here this might be your only bottleneck in some cases. You should always think in perspective what if you had even bigger number ? Would your method still work fast enough ? A working program doesn't mean that it's a good program.
Next you start thinking what can you improve and why exactly you are doing certain things. Do you really need to know every single number that is prime ?
In my solution there is simple prime factorization starting from the smallest prime we keep increasing the current number we have until we get a number that we can divide only by itself i.e the largest prime factor. 100,000 iterations run for about 3000-3200 ms on my machine.
Code Snippets
static void Main(string[] args)
{
long num = 600851475143;
int count = 3;
Stopwatch sw = Stopwatch.StartNew();
while (num > 1)
{
if (num%count == 0)
{
num /= count;
}
else
{
count += 2;
}
}
sw.Stop();
Console.WriteLine("The largest prime factor of the number {0} is {1} ", 600851475143, count);
Console.WriteLine("Time to calculate in milliseconds : {0}", sw.ElapsedMilliseconds);
Console.ReadKey();
}Context
StackExchange Code Review Q#143651, answer score: 4
Revisions (0)
No revisions yet.