patterncsharpModerate
Largest Prime Factor (PE3) using OOP
Viewed 0 times
largestusingpe3factorprimeoop
Problem
I want to learn some C# syntax/paradigms (I'm more used to Java), and have been wanting to get a bit better at math as well, so I solved ProjectEuler3: Largest prime factor with the following small program in LINQPad. It gives the correct answer and completes in 0.05s-0.08s.
For reference, here is the problem statement:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
All feedback welcomed. In particular, are there some C# mistakes I am making or features I am missing on due to being very new at it?
I'm also not that great at math at the moment, if you know of edge cases that I may have missed with the calculations below, please let me know so I can improve my math knowledge a bit as well!
P.S.: Note the
```
void Main()
{
Console.WriteLine("ProjectEuler3: Largest prime factor");
BigInteger testCase = 600851475143;
ProjectEuler3 PE3 = new ProjectEuler3(testCase);
Console.WriteLine("Prime factor of {0} is: {1}", testCase, PE3.GetAnswer());
}
class ProjectEuler3
{
private BigInteger number;
public ProjectEuler3(BigInteger number)
{
this.number = number;
}
public BigInteger GetAnswer()
{
// largest possible prime factor of a number is its square root [citation needed]
BigInteger maxPrimeFactor = BigNumbersUtils.Sqrt(number);
// make sure number we start from is odd, as even numbers are never going to be prime
if (maxPrimeFactor % 2 == 0) { maxPrimeFactor += 1; }
// iterating by 2s to skip even numbers
for (BigInteger i = maxPrimeFactor; i >= 1; i = i - 2)
{
if (IsFactor(i, number) && IsPrime(i))
{
return i;
}
}
return 1;
}
priva
For reference, here is the problem statement:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
All feedback welcomed. In particular, are there some C# mistakes I am making or features I am missing on due to being very new at it?
I'm also not that great at math at the moment, if you know of edge cases that I may have missed with the calculations below, please let me know so I can improve my math knowledge a bit as well!
P.S.: Note the
BigNumbersUtils.Sqrt extension method comes from this Stack Overflow answer. I am excluding it since it is not my code. it gives the same result as Math.Sqrt but for BigInteger type.```
void Main()
{
Console.WriteLine("ProjectEuler3: Largest prime factor");
BigInteger testCase = 600851475143;
ProjectEuler3 PE3 = new ProjectEuler3(testCase);
Console.WriteLine("Prime factor of {0} is: {1}", testCase, PE3.GetAnswer());
}
class ProjectEuler3
{
private BigInteger number;
public ProjectEuler3(BigInteger number)
{
this.number = number;
}
public BigInteger GetAnswer()
{
// largest possible prime factor of a number is its square root [citation needed]
BigInteger maxPrimeFactor = BigNumbersUtils.Sqrt(number);
// make sure number we start from is odd, as even numbers are never going to be prime
if (maxPrimeFactor % 2 == 0) { maxPrimeFactor += 1; }
// iterating by 2s to skip even numbers
for (BigInteger i = maxPrimeFactor; i >= 1; i = i - 2)
{
if (IsFactor(i, number) && IsPrime(i))
{
return i;
}
}
return 1;
}
priva
Solution
Project Euler questions are primarily about math. This isn't really the place to practice object-oriented programming. In fact, trying to model the problem literally will lead you to a suboptimal solution.
The "right" algorithm is very simple, and involves no primality testing.
I wouldn't bother with an
The "right" algorithm is very simple, and involves no primality testing.
I wouldn't bother with an
IsFactor() method at all — if you just write it as a modulo test, there is no confusion about the order the parameters.class ProjectEuler3
{
private long number;
public ProjectEuler3(long number)
{
this.number = number;
}
public long GetAnswer()
{
long n = this.number;
while (n % 2 == 0)
{
n /= 2;
}
for (long factor = 3; factor < n; factor += 2)
{
while (n % factor == 0)
{
n /= factor;
}
}
return n;
}
}Code Snippets
class ProjectEuler3
{
private long number;
public ProjectEuler3(long number)
{
this.number = number;
}
public long GetAnswer()
{
long n = this.number;
while (n % 2 == 0)
{
n /= 2;
}
for (long factor = 3; factor < n; factor += 2)
{
while (n % factor == 0)
{
n /= factor;
}
}
return n;
}
}Context
StackExchange Code Review Q#133078, answer score: 14
Revisions (0)
No revisions yet.