mcgee1095
mcgee1095

Reputation: 5

Why is my solution to project euler 1 not working?

I decided to just try and get the small example of only going to 10 like the example shown.

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.

public class project1 {

    public static void main(String[] args) {
        int three=0;
        int tot3=0;
        int five=0;
        int tot5=0;
        int total;
        
        while (tot3<10) {
            three+=3;
            tot3=tot3+three;
        };
        while (tot5<10) {
            five+=5;
            tot5=tot5+five;
        };
        
        total=tot3+tot5;
        
        System.out.println("Three's: " + tot3);
        System.out.println("Five's: " + tot5);
        System.out.println("Combined: " + total);
    }

}

My output is as show:

Three's: 18
Five's: 15
Combined: 33

Upvotes: 0

Views: 295

Answers (7)

dasunse
dasunse

Reputation: 3089

Try this

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            long n = in.nextLong()-1;
            System.out.println((n-n%3)*(n/3+1)/2 + (n-n%5)*(n/5+1)/2 - (n-n%15)*(n/15+1)/2);
        }
    }
}

Upvotes: 0

user2264205
user2264205

Reputation: 16

public class project1 {

    public static void main(String[] args) {
        int number = 0;
        int total = 0;

        while (number < 10) {
            System.out.println(number);

            if ((number % 3) == 0) {
                System.out.println(number + " is a multiple of 3");
                total = total + number; 
            }
            else if ((number % 5) == 0) {
                System.out.println(number + " is a multiple of 5");
                total = total+number;   
            }
            number++;
        }
        System.out.println("total = "+ total);
    }
}

Looking at how slow I was, I did roughly the same thing as everyone else but swapped to a modulus function. The modulus function gives you the remainder(int) of dividing the first number by the second number, and can be compared to another integer. Here I have used it to check if the current number is directly divisible by 3 or 5, and add it to the total if the value is true.

Upvotes: 0

RaymondMachira
RaymondMachira

Reputation: 354

Keep track of your variables through the loop and you'll see the problem: for tot3

  • =3
  • =9
  • =18
  • =30

You're keeping track of the sum, instead of tracking the multiples. This problem is partially solved in by

while(three<10)

Again, keeping track of the variable through the loop you'll see that this is wrong- it stops at 12, not 9 as you want it. Change it to

While(three<9) //ie the last divisible number before the limit, or that limit if its divisible (in the case of 5)

All said, an infinitely more elegant solution would involve modulus and a nice little if statement. I hope this helps!

Upvotes: 1

J.Miller
J.Miller

Reputation: 487

I would recommend looking into using the modular operator to solve this problem. In java % will allow you to perform modular arithmetic. For example any multiple of 3 such as 9 % 3 = 0 while 9 % 2 = 1. It can be thought of as what remains after you divide the first number by the second. All multiples of a number modded by that number will return zero.

Upvotes: 1

Patashu
Patashu

Reputation: 21773

First,

while (tot3<10) {
    three+=3;
    tot3=tot3+three;
};
while (tot5<10) {
    five+=5;
    tot5=tot5+five;
};

This should be

while (three<10) {
    three+=3;
    tot3=tot3+three;
};
while (five<10) {
    five+=5;
    tot5=tot5+five;
};

Because you're concerned about when you start counting numbers above 10, not when your TOTAL of those numbers is above 10.

Secondly, your solution will count numbers that are a multiple of three and of five twice. For example, 15 will be added twice. Learn about the modulo operator, %, to come up with a solution to this (for example, not adding five to the tot5 count if five % 3 == 0)

Upvotes: 1

John3136
John3136

Reputation: 29266

while (tot3<10) {
        three+=3;
        tot3=tot3+three;
};

I think you mean

while (tot3<10) {
        three += tot3; // Add this multiple of 3 to the total.
        tot3+=3;       // increment the "next multiple"
    }

(same for 5)

Lone nebula also makes a good point - you'd need to add logic to the "5" loop to check it's not already counted in the 3 loop. The mod (%) operator can help there.

Upvotes: 1

Lone nebula
Lone nebula

Reputation: 4878

Numbers that are both multiples of 3 and 5 (like 15 for instance), are counted twice - once in each loop.

Upvotes: 3

Related Questions