1.Natural Numbers

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.

The term “natural number” refers either to a member of the set of positive integers 1, 2, 3,) or   to the set of non-negative integers 0, 1, 2, 3, ….

set name symbol
…, -2, -1, 0, 1, 2, … integers Z
1, 2, 3, 4, .. positive integers Z-+
0, 1, 2, 3, 4, … nonnegative integers Z-*
0, -1, -2, -3, -4, … nonpositive integers
-1, -2, -3, -4, … negative integers Z–

public class Problem1 {

    public void naturalNumberPE() {
        long target = 1000000000l-1;
        long p1 = SumDivisibleBy(3,target);
        //System.out.println(p1);
        long p2 = SumDivisibleBy(5,target);
        //System.out.println(p2);
        long p3 = SumDivisibleBy(15,target);
        //System.out.println(p3);
        long fp = p1+p2-p3;
        System.out.println(fp);
    }
    public long SumDivisibleBy(long n, long target) {
        long p= target / n;
        return (n*(p*(p+1))) /2;
    }

    public static void main(String[] args) {
        Problem1 p = new Problem1();
        p.naturalNumberPE();
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: