Skip to content

Instantly share code, notes, and snippets.

@RohithKumar1368
Last active August 16, 2017 14:16
Show Gist options
  • Save RohithKumar1368/0bcb8a9bcf21b1a60e21cdf29472197d to your computer and use it in GitHub Desktop.
Save RohithKumar1368/0bcb8a9bcf21b1a60e21cdf29472197d to your computer and use it in GitHub Desktop.
Project Euler 1 : To find the sum of all multiples of 3 or 5 below n.
#include<stdio.h>
int main(){
int n ;
int sum ;
scanf("%d",&n) ;
int three = (n-1) / 3 ; // no of 3 multiples below n
int five = (n-1) / 5 ; // no of 5 multiples below n
int fifteen = (n-1) / 15 ; // no of 15 multiples below n
sum = 3 * (three * (three+1)) / 2 ; // finding sum of all multiples of 3 below n
sum += 5 * (five * (five+1)) / 2 ; // finding sum of all multiples of 3 below n
sum -= 15 * (fifteen * (fifteen+1)) / 2 ; // finding sum of all multiples of 3 below n
printf("%d\n",sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment