Sal
Sal

Reputation: 1691

Project Euler 5, answer I got is only half, why?

I am working on Project Euler number 5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

However, the answer I keep getting, 116396280, is only half of the actual answer. Where am I going wrong? Why am I getting only half of the answer? This is in java by the way.

public class main{
    public static void main(String[] args){
        long number=2520;//smallest number divisible by all numbers from 1-10
        long x;//for loop counter
        for (x=19;x>10;x-=2){
            if (x!=15){//its factors 5 and 3 have already been counted
                number*=x;//multiplies new prime numbers
            }
        }
       System.out.println(number);
    }
}

Upvotes: 0

Views: 243

Answers (1)

Dolda2000
Dolda2000

Reputation: 25855

By only walking through the odd numbers, you are missing out on 16, whose factorization is 24 Your answer only contains 23.

Upvotes: 1

Related Questions