Jump to content

Question about my algorithm for Problem 10, Project euler !

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Kushtrim

Kushtrim

    Newbie

  • Members
  • PipPip
  • 14 posts
Hello!
I'm trying to solve Problem 10 of ProjectEuler.net.

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

This is my Code:

/**

 * Write a description of class ff here.

 * Problem 010: 142913828922

 *                

 * @author (your name) 

 * @version (a version number or a date)

 */

public class yy

{

   public static void main (String args[])

  {  int sum=0;

      int limit=2000000;

     boolean A[] = new boolean[limit];

     A[0]=false;

     A[1]=false;

    for (int m=2;m<limit;m++)

    {

        A[m]=true;

    }

   

    for( int i=2 ; i<limit;i++)

    {

        if( A[i] == true)

        {

            for (int j=i+i; j<limit; j+=i)

            {

                A[j]=false;

            }

        }

         

    }

    

     for (int m=2 ;m<limit;m++)

    {

        if ( A[m] == true)

       { sum+=m; }

    }

    System.out.println(sum + "");

    }

  }

 
The algorithm is based on Sieve of Eratosthenes.
I've tested it for values of limit 10,20 and it works, but when the input is the one from the problem ( two million ) the answer is wrong :bad:


Could someone please have a look the code, and tell me where is the bug :rolleyes:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
One possible issue: what is the maximum value an int can store before rolling over?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Kushtrim

Kushtrim

    Newbie

  • Members
  • PipPip
  • 14 posts
Wooops !:w00t:
Now, i tried to declare sum as long, and voila , the answer is correct :D
Thank you :) I spend almost an hour just staring at the code trying to find that bug.

Though I'm quite surprised, shouldn't Java give me an error when the int reaches it's limit, and nothing else can be added to it :cursing:

Thanks again:)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Nope, it becomes a weird ****ty number :P

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
When I was doing the problems in C++, I regularly had to use the gnump library to get correct answers.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users