document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package project.euler;
  2.  
  3. import java.math.BigInteger;
  4.  
  5. public class ProblemSets
  6. {
  7. /*
  8.  ============================================================================
  9.  Name        : Project EULER problem 1: Multiples of 3 and 5
  10.  Author      : Catarina Moreira
  11.  Copyright   : Catarina Moreira all rights reserved
  12.  Description : If we list all the natural numbers below 10 that are multiples of 3
  13.               or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  14.               Find the sum of all the multiples of 3 or 5 below 1000.
  15.  ============================================================================
  16. */
  17. public BigInteger findMultiples( BigInteger limit, int k1, int k2 )
  18. {  
  19.     BigInteger sum_k1 = closedSum(limit, k1);
  20.     BigInteger sum_k2 = closedSum(limit, k2);
  21.     BigInteger sum_k1k2 = closedSum(limit, k1*k2);
  22.        
  23.     return sum_k1.add(sum_k2).subtract(sum_k1k2);
  24. }
  25.    
  26. public BigInteger closedSum( BigInteger limit, int k )
  27. {
  28.     BigInteger result = BigInteger.ZERO;
  29.     BigInteger i =  BigInteger.ONE;
  30.        
  31.     BigInteger k_big = new BigInteger( Integer.toString(k) );
  32.     BigInteger seriesLimit = limit.subtract( BigInteger.ONE ).divide( k_big );
  33.        
  34.     while( i.compareTo(seriesLimit) <= 0 )
  35.     {
  36.         result = result.add(i.multiply( new BigInteger( Integer.toString(k) )));
  37.     i = i.add(BigInteger.ONE);
  38.     }
  39.    
  40.     return result;
  41.     }
  42. }
');