Thursday, May 7, 2020

PROJECT EULER SOLUTION MULTIPLES OF 3 & 5

SOLUTION TO THE PROBLEM GIVEN IN PROJECT EULER PROBLEM NO 2 ---Multiples of 3 and 5


ALGORITHM :

Get MULTIPLES of 3 and5 up to the NO given, SUPPOSE x in the CODE given below and ADD them until it CROSSES THE NO x . Now taking MULTIPLES of both 3 and 5 includes taking multiples of 15 twice times , one for 3's multiple and the other for 5's multiple. so we have to subtract the multiples of 15 until it crosses the number x


SOLUTION :


#include <bits/stdc++.h>using namespace std;

long long int Sum_Divisor(long long int n){
    //long long int i=1;    long long int sum =0;
    for (long long int i = 1; i < n/2; ++i) {
        if (3*i < n)
            sum=sum+3*i;
        if (5*i < n)
            sum=sum+5*i;
        if (3*5*i < n)
            sum=sum-3*5*i; // subtract the multipes of 15     }
    cout<<sum;
}

int main()
{
    long long int x;
    cin>>x; 
    Sum_Divisor(x);
}


for the above QUESTION IN EULER PROJECT x = 1000 , and the ans is  233168 .

No comments:

Post a Comment