It's been fun reading responses in the last two Project Euler threads. If you missed them, you can view Problem #1 and Problem #2.
Let's keep it rolling with Problem #3. Again, this is from the wonderful Project Euler.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Look forward to seeing solutions in your language of choice!
Top comments (21)
Ruby✨💎✨
Nice one. But, I think y << prime, to_a.last pose addition execution time. Michael Kohl's approach is more optimized.
C :))
C is a powerful language. The same algorithm in Ruby shows to have time complexity issue. Good code, bro!
Java
PHP;
Javascript;
Author: Brewyn
Largest Prime Factor
Javascript
PHP
Python
Java
Rust:
Result:
Simple Rust solution
Python 3 solution. Inefficient I know...
Scala:
PHP, again.
Solution: 6857
/*Largest prime factor
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?*/
include
using namespace std;
int main()
{
long long num=600851475143 ,largest=1 ;
for(int i=2;i<=num;i++)
{
if(num%i==0)
{
num=num/i;
cout< if(i>largest)
largest=i;
}
}
cout<<"\n\n"<<"the largest prime factor of the number 600851475143 is "<<largest<<"\n";
return 0;
}
My code in C++ >>Output>>6857