Problem
#include <iostream> // triangular number:∑x=1+2+..+x=x∗(x+1)/2 unsigned long long sum(unsigned long long x) { return x * (x + 1) / 2; } int main() { unsigned int tests; std::cin >> tests; while (tests––) { unsigned long long last; std::cin >> last; // not including that number last––; // find sum of all numbers divisible by 3 or 5 auto sumThree = 3 * sum(last / 3); auto sumFive = 5 * sum(last / 5); // however, those numbers divisible by 3 AND 5 will be counted twice auto sumFifteen = 15 * sum(last / 15); std::cout << (sumThree + sumFive – sumFifteen) << std::endl; } return 0; }
public final class p001 implements EulerSolution { public static void main(String[] args) { System.out.println(new p001().run()); } /* * Computers are fast, so we can implement this solution directly without any clever math. * A conservative upper bound for the sum is 1000 * 1000, which fits in a Java int type. */ public String run() { int sum = 0; for (int i = 0; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) sum += i; } return Integer.toString(sum);
// 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 1000. var total = 0; for (var i = 1; i < 1000; i++) { if (i % 3 === 0 || i % 5 === 0) { total += i; } } console.log(total)
HackerRank
see
My.
References
projecteuler.net/thread.pl (written by Gustaf Erikson)
Its not my first time to pay a visit this web page, i am visiting this website
dailly and obtain good information from here all the time.