Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Project Euler problem one

  1. #1
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Project Euler problem one

    I am using Project Euler to test myself on the Java programming that I am teaching myself and here is problem one: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.


    Here is my Java attempt at calculating it
    public class problemOne {
        public static void main(String[] args) {
            int count;
            int sum = 0;
    //Multiples of 3
            for(count = 0; count < 1000; count += 3){
                System.out.println("count is: " + count);
                sum = sum + count;
            }
    //Multiples of 5
            for(count = 0; count < 1000; count += 5){
                System.out.println("count is: " + count);
                sum = sum + count;
            }
            System.out.println("Total is: " + sum);
        }
    }
    I get 26333 as the answer but when I enter that at projecteuler.net it says incorrect.
    Is there an error in the logic that I used to calculate? If so, where is it so I can rethink it.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Project Euler problem one

    You're forgetting that some numbers are multiples of *both* 3 and 5, and you are in effect adding these numbers *twice*.

  3. #3
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Project Euler problem one

    Quote Originally Posted by curmudgeon View Post
    You're forgetting that some numbers are multiples of *both* 3 and 5, and you are in effect adding these numbers *twice*.
    Thanks

  4. #4
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Project Euler problem one

    I got it figured out! Now on to problem 2...

Similar Threads

  1. [SOLVED] Project Euler Questio 2
    By goldenpsycho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2012, 07:25 AM
  2. Euler Paths with dfs algorithm problem
    By claudio.r in forum Algorithms & Recursion
    Replies: 18
    Last Post: March 22nd, 2012, 04:39 PM
  3. [SOLVED] Project Euler: Truncatable Primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 10th, 2011, 12:27 PM
  4. Euler integration
    By Kakashi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2009, 04:19 PM
  5. Project Euler
    By helloworld922 in forum The Cafe
    Replies: 5
    Last Post: September 9th, 2009, 02:51 PM