Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project.euler;
- import java.math.BigInteger;
- public class ProblemSets
- {
- /*
- ============================================================================
- Name : Project EULER problem 1: Multiples of 3 and 5
- Author : Catarina Moreira
- Copyright : Catarina Moreira all rights reserved
- Description : 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 BigInteger findMultiples( BigInteger limit, int k1, int k2 )
- {
- BigInteger sum_k1 = closedSum(limit, k1);
- BigInteger sum_k2 = closedSum(limit, k2);
- BigInteger sum_k1k2 = closedSum(limit, k1*k2);
- return sum_k1.add(sum_k2).subtract(sum_k1k2);
- }
- public BigInteger closedSum( BigInteger limit, int k )
- {
- BigInteger result = BigInteger.ZERO;
- BigInteger i = BigInteger.ONE;
- BigInteger k_big = new BigInteger( Integer.toString(k) );
- BigInteger seriesLimit = limit.subtract( BigInteger.ONE ).divide( k_big );
- while( i.compareTo(seriesLimit) <= 0 )
- {
- result = result.add(i.multiply( new BigInteger( Integer.toString(k) )));
- i = i.add(BigInteger.ONE);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement