Filter löschen
Filter löschen

Project Euler Problem 3

5 Ansichten (letzte 30 Tage)
Shahrear Khan Faisal
Shahrear Khan Faisal am 21 Okt. 2020
Beantwortet: Manvi Goel am 28 Okt. 2020
I'm trying to solve the Project Euler problem of largest prime factor in MATLAB cody. My code gives the correct answer but cody says the solution is wrong. Could anyone please point out the mistake in my code?
The problem description is -
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number being input, input might be uint64 for large numbers, out must be double precision?
function y = euler003(x)
a = [];
for i = 1:x
if rem(x,i) == 0
a = [a i];
end
end
p = [];
for i = 1:length(a)
if isprime(a(i))
p = [p a(i)];
end
end
y = max(p);
end

Antworten (1)

Manvi Goel
Manvi Goel am 28 Okt. 2020
The issue with MATLAB Cody not accepting your submission could be with the time complexity of your code. The sample test cases attached are big numbers and your code might not run for them.
You can refer to this link https://www.geeksforgeeks.org/find-largest-prime-factor-number/ for an optimised approach for finding the prime factors.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by