Sum of Multiples of 3 or 5
Find the sum of all numbers below n that are multiples of 3 or 5 (classic warm-up).
Sample input
10
Sample output
23
Solution
n = 10
total = sum(i for i in range(1, n) if i % 3 == 0 or i % 5 == 0)
print(total)
const n = 10;
let total = 0;
for (let i = 1; i < n; i++) {
if (i % 3 === 0 || i % 5 === 0) total += i;
}
console.log(total);
public class Main {
public static void main(String[] args) {
int n = 10, total = 0;
for (int i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) total += i;
}
System.out.println(total);
}
}
fun main() {
val n = 10
val total = (1 until n).filter { it % 3 == 0 || it % 5 == 0 }.sum()
println(total)
}
let n = 10
let total = (1..<n).filter { $0 % 3 == 0 || $0 % 5 == 0 }.reduce(0, +)
print(total)
void main() {
int n = 10, total = 0;
for (var i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) total += i;
}
print(total);
}
#include <iostream>
using namespace std;
int main() {
int n = 10, total = 0;
for (int i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) total += i;
}
cout << total << endl;
return 0;
}
#include <stdio.h>
int main() {
int n = 10, total = 0;
for (int i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) total += i;
}
printf("%d\n", total);
return 0;
}