This page looks best with JavaScript enabled

HackerRank - Project Euler #1: Multiples of 3 and 5

 ·  ☕ 2 min read  ·  ✍️ devoalda

Introduction

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 N.

Input and Output Format

1
2
3
4
5
6
7
# Input:
2
10
100
# Output:
23
2318

{: file="Input and Output” }

Explanation

For N = 10, 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.
Similarly for N = 100, we get 2318.

Process

Using a for loop to loop through all the numbers below N, and check if the number is a multiple of 3 or 5, if it is, add it to the sum, and print out the sum at the end of the loop.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void sumMultiple(int n);

int main(){
    int t;
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n;
        scanf("%d",&n);
        sumMultiple(n);
        printf("\n");
    }
    return 0;
}

void sumMultiple(int n){
    int sum = 0;
    for(int i = 1; i< n; i++){
        if(i%3 == 0 || i% 5 == 0){
            sum += i;
        }

    }
    printf("%d", sum);
}

{: file="Multiples of 3 and 5.c” }

Afterthoughts

This solution was a bit of a brute force solution, but it worked. It passed 4/6 test cases and needed to be optimized. I’m sure there’s a more efficient way to do this, but I’ll take a second look at this later.

References

Share on

Devoalda
WRITTEN BY
devoalda
Technophile