Project Euler #1: Multiples of 3 and 5 HackerRank Solution In C -Language | | Hacker rank

Project Euler #1: Multiples of 3 and 5 Hacker Rank Solution In C -Language | | Hacker rank

Question:

If we list all the natural numbers below  that are multiples of  or , we get  and . The sum of these multiples is .
Find the sum of all the multiples of  or  below .

Input Format
First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .
Constraints
Output Format
For each test case, print an integer that denotes the sum of all the multiples of  or  below .

Answer


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t;
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n;
        scanf("%d",&n);
        long int counter=0,i,j,k,t3,t5,t15;
        t3=(n-1)/3;
        t5=(n-1)/5;
        t15=(n-1)/15;
        i=t3*(6+(t3-1)*3);
        j=t5*(10+(t5-1)*5);
        k=t15*(30+(t15-1)*15);
        counter=(i+j-k)/2;
        printf("%ld\n",counter);
    }
    return 0;
}

Comments :

Post a Comment