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 ...
For further actions, you may consider blocking this person and/or reporting abuse
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
Python!
C#
Result
Julia
function isprime(n)
a = ceil(Int,sqrt(n))+1
a=iseven(a) ? a+1 : a
for i = a:-2:3
if n % i == 0
return false
end
end
return true
end
function largest_prime(number)
sqrt_of_n = ceil(Int,sqrt(number)) # the answer must be less than square root of n
sqrt_of_n=iseven(sqrt_of_n) ? sqrt_of_n+1 : sqrt_of_n #a prime cannot be even, so only check odd numbers
for i = sqrt_of_n:-2:1
if number % i == 0 #if 600851475143 is divisible by i
if isprime(i) #then check if i is prime
return i
break
end
end
end
end
println(largest_prime(600851475143))
How do I enter formatted code on this site?
Python:
It returns
2
whenn = 1
, my code returnsnil
.java is cool
static long IsPrime(long n){
int counter = 2;
long newval = n;
long larfact = 0;
while(counter*counter <= n){
if(newval % counter == 0){
newval = newval/counter;
larfact = counter;
}
else{
counter++;
}
}
if(newval > larfact){
larfact = newval;
}
return larfact;
but 600851475143 is coming under the range of the "long" data type of java but still it works for any value below it .
THE APPROACH IS PRETTY SIMPLE :
I used the the "Fundamental Theorem of Arithmetic" which states That Any Integer greater than 1 is either a prime number or product of prime numbers.
For example:
12
we start with smallest prime number 2.
the 12/2 = 6 which means that 2 is the prime factor 12.
If we try again by dividing the result by 2
6/2 = 3 . 3 is also a prime number . So the complete factorization is 2,2,3.
I have implemented this logic in the above code This works really fast .I think this code can be improved
a little bit Please some one suggest me.
Hope You like The implementation ;)
python
Well optimized code. You had to think a lot. Good one, bro!