[Java] What is the smallest number from 1-20 that is divisible without a remainder%

percocet

Member
Oct 21, 2016
72
16
Hey,
I could ask this on stackexchange or just straight up find the answer online, but what fun would that be. Plus this is an opportunity to get to know everybody in the community.

So i've decided to try the projecteuler problems again (this time in java, not python) and am having trouble. Hopefully someone can check my logic

Code:
 public static int p003(){
        int x = 1;
        while (remainderChecker(x) == false){
             x++;
         }
        
 return x;
     }

    public static boolean remainderChecker(int x){
        for (int i = 1; i<=20; i++){
            if(x%i > 0){
               return false;
            }else
            i++;     
        }
        return true;
    }
    
}
 

percocet

Member
Oct 21, 2016
72
16
What are you trying to achieve? Won't it always return 1, as 1 is divisible by 1 without a remainder?
I'm trying to find the first number in a set of N (let n be all real natural numbers) where divided any and all integers from 1-10 doesn't return a remainder.

It wouldn't be 1 because 1/2 returns a remainder.
For example if I was looking for 1-10. The number would be 2520. Because each number from 1-10 can be divided by 2520 without returning a remainder.
 

CosmoPeak

PeakRP.com
May 15, 2016
271
268
I'm trying to find the first number in a set of N (let n be all real natural numbers) where divided any and all integers from 1-10 doesn't return a remainder.

It wouldn't be 1 because 1/2 returns a remainder.
For example if I was looking for 1-10. The number would be 2520. Because each number from 1-10 can be divided by 2520 without returning a remainder.
Oh right, I see. Your logic seems good. Are you having any issue in particular?

edit:

Code:
for (int i = 1; i<=20; i++){
    if(x%i > 0){
        return false;
    }else
        i++;
}

Here you are incrementing i twice, once in the for loop header and once in the body at the end. Remove the one at the end.

You can shorten it to:

Code:
for (int i = 1; i <= 20; i++) {
    if(x % i > 0)
        return false;
}
 

percocet

Member
Oct 21, 2016
72
16
Oh right, I see. Your logic seems good. Are you having any issue in particular?

edit:

Code:
for (int i = 1; i<=20; i++){
    if(x%i > 0){
        return false;
    }else
        i++;
}

Here you are incrementing i twice, once in the for loop header and once in the body at the end. Remove the one at the end.

You can shorten it to:

Code:
for (int i = 1; i <= 20; i++) {
    if(x % i > 0)
        return false;
}

You were right aha, its because I'm incrementing i++ twice. Therefore the solution was just to get rid of i++ in my second class. I feel so silly. Thanks for your help.
 

Users who are viewing this thread

Top