Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7607597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:42:15+00:00 2026-05-31T00:42:15+00:00

I was trying to solve Project Euler problem 10 using python, but my program

  • 0

I was trying to solve Project Euler problem 10 using python, but my program gave a wrong result. Since I am a complete noob to python and I could not find any fault in my (apparently brute-force) logic, I wrote a program in java (almost translated it), and it gave a different result, which turned out to be right.

Here is the python code:

from math import *

limit = 2000000

def isPrime(number):
    if number == 2: return 1
    elif number % 2 == 0: return 0
    elif number == 3: return 1
    elif number == 5: return 1
    elif number == 7: return 1
    else:
        rootOfNumber = sqrt(number)
        tag = 3
        while tag < rootOfNumber:
            if number % tag != 0:
                tag += 2
            else:
                break           ###   
        if tag >= rootOfNumber: ###EDIT: it should by only tag > rootOfNumber here
            return 1            ###       Thats what the problem was.
        else:
            return 0

sum = 2 # 2 is an even prime, something we are not iterating for
for i in range(3, limit, 2):
    if isPrime(i) == 1:
        sum += i

print(sum)
print('done...')

The equivalent java code is:

public class Prob10{
static int limit = 2000000;
static long sum = 2L; // 2 is an even prime, something we are not iterating for

public static void main (String[] args) {
    for(int i = 3; i < limit; i+=2) {
    if( isPrime(i) ) 
        sum += i;
    }
    System.out.println(sum);
}

private static boolean isPrime (int number) {
    if (number == 2) return true;
    else if (number == 3 || number == 5 || number == 7) return true;
    else {
        double rootOfNumber = Math.sqrt(number);
        int tag = 3;
        while (tag < rootOfNumber) {
            if (number % tag != 0) 
                tag +=2;
            else
                break;
        }
        if (tag > rootOfNumber)
            return true;
        else
            return false;
    }
}

}

I think I am doing some silly mistake or missing some subtle point.

p.s. I know my isPrime implementation is not too good. I am not printing the outputs because it may spoil the problem for others.

Any comments about (bad) style in the python program are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:42:16+00:00Added an answer on May 31, 2026 at 12:42 am

    Try running with your code for example isPrime(49). You should figure out your problem from there. You have replaced a > with a >= in if (tag > rootOfNumber)
    .Also as some coding style, you could just replace the first lines with:

    if i in (2, 3, 5, 7): return 1
    elif number % 2 == 0: return 0
    else:
        ......
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a little context, i'm trying to solve Project Euler problem 31 using excellent
I'm trying to solve Problem 13 of project euler, which involves the sum of
I'm trying to solve problem 50 on Project Euler . Don't give me the
I am trying to solve Project Euler Problem 88 , and I did it
I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I
I have been trying to solve Project Euler's problem #59 for a while, and
I'm trying to solve Problem #5 in Project Euler. The code works for the
I'm trying to solve problem 14 on project euler, and my initial brute force
I am trying to solve Project Euler problem #12: The sequence of triangle numbers
I am trying to solve Q10 from Project Euler. I am using JavaScript and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.