বৃহস্পতিবার, ২১ জুন, ২০১২

Project Euler : Problem 1 Solution

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.


<pre class="prettyprint">

public class Problem1 {
   
    public static void main(String args[]){
   
        System.out.println("\nTotal "+countNoOfMultipleOfThreeNFive(1000));
    }
    private static int countNoOfMultipleOfThreeNFive(int boundary){
    int count =0;
        for(int i =1;i<boundary;i++){
            if(checkIfDividible(i))
                count+=i;
         }
        return count;
    }
    private static boolean checkIfDividible(int n){
    if((n%3 ==0)||(n%5==0)){
        System.out.print(" "+n);
        return true;
    }
    return false;
       
    }
}
</pre>

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন