Euler Project #3 Largest prime factor Timeout
I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor.
Question: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of a given number N?
I have written two different codes which both works for the first four problems in HackerRank but timeout on the last two. Please let me know how I can optimize my code further so that the timeout doesn't occur.
Code #1
for a in range(int(input())):
n = int(input())
b = n//2
i=1
if b%2==1:
i+=1
max=1
while b > 2:
if n%b==0:
isprime = True
for c in range(2,int(b**0.5)+1):
if b%c==0:
isprime = False
break;
if isprime:
max = b
break;
b-=i
if max==1:
max=n
print(max)
This one tries to find the first largest numbers which can divide a particular number and then tries to find whether it is a prime.
Code #2
for a in range(int(input())):
n = int(input())
num = n
b = 2
max=1
while n > 1 and b < (num//2)+1:
if n%b==0:
n//=b
max=b
b+=1
if max==1:
max=n
print(max)
This basically tries to find the last largest factor by multiple division.
python beginner python-3.x programming-challenge
add a comment |
I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor.
Question: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of a given number N?
I have written two different codes which both works for the first four problems in HackerRank but timeout on the last two. Please let me know how I can optimize my code further so that the timeout doesn't occur.
Code #1
for a in range(int(input())):
n = int(input())
b = n//2
i=1
if b%2==1:
i+=1
max=1
while b > 2:
if n%b==0:
isprime = True
for c in range(2,int(b**0.5)+1):
if b%c==0:
isprime = False
break;
if isprime:
max = b
break;
b-=i
if max==1:
max=n
print(max)
This one tries to find the first largest numbers which can divide a particular number and then tries to find whether it is a prime.
Code #2
for a in range(int(input())):
n = int(input())
num = n
b = 2
max=1
while n > 1 and b < (num//2)+1:
if n%b==0:
n//=b
max=b
b+=1
if max==1:
max=n
print(max)
This basically tries to find the last largest factor by multiple division.
python beginner python-3.x programming-challenge
add a comment |
I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor.
Question: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of a given number N?
I have written two different codes which both works for the first four problems in HackerRank but timeout on the last two. Please let me know how I can optimize my code further so that the timeout doesn't occur.
Code #1
for a in range(int(input())):
n = int(input())
b = n//2
i=1
if b%2==1:
i+=1
max=1
while b > 2:
if n%b==0:
isprime = True
for c in range(2,int(b**0.5)+1):
if b%c==0:
isprime = False
break;
if isprime:
max = b
break;
b-=i
if max==1:
max=n
print(max)
This one tries to find the first largest numbers which can divide a particular number and then tries to find whether it is a prime.
Code #2
for a in range(int(input())):
n = int(input())
num = n
b = 2
max=1
while n > 1 and b < (num//2)+1:
if n%b==0:
n//=b
max=b
b+=1
if max==1:
max=n
print(max)
This basically tries to find the last largest factor by multiple division.
python beginner python-3.x programming-challenge
I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor.
Question: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of a given number N?
I have written two different codes which both works for the first four problems in HackerRank but timeout on the last two. Please let me know how I can optimize my code further so that the timeout doesn't occur.
Code #1
for a in range(int(input())):
n = int(input())
b = n//2
i=1
if b%2==1:
i+=1
max=1
while b > 2:
if n%b==0:
isprime = True
for c in range(2,int(b**0.5)+1):
if b%c==0:
isprime = False
break;
if isprime:
max = b
break;
b-=i
if max==1:
max=n
print(max)
This one tries to find the first largest numbers which can divide a particular number and then tries to find whether it is a prime.
Code #2
for a in range(int(input())):
n = int(input())
num = n
b = 2
max=1
while n > 1 and b < (num//2)+1:
if n%b==0:
n//=b
max=b
b+=1
if max==1:
max=n
print(max)
This basically tries to find the last largest factor by multiple division.
python beginner python-3.x programming-challenge
python beginner python-3.x programming-challenge
asked 8 mins ago
remedcuremedcu
1004
1004
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211361%2feuler-project-3-largest-prime-factor-timeout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211361%2feuler-project-3-largest-prime-factor-timeout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown