fork(2) download
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created by studyalgorithms
  5.  */
  6. class Problem1 {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. Scanner sc = new Scanner(System.in);
  11. long t = sc.nextLong();
  12. for (long i = 0; i < t; i++) {
  13. long n = sc.nextLong();
  14. long a = 0, b = 0, d = 0;
  15.  
  16. // Here a, b and d are the count of numbers divisible by 3, 5 and 15 respectively
  17. a = (n - 1) / 3;
  18. b = (n - 1) / 5;
  19. d = (n - 1) / 15;
  20.  
  21. // To get the sum of all numbers divisible by 3 (sum3) i.e. 3+6+9+-----+3n = 3(1+2+3+---+n) = 3*n(n+1)/2
  22. // Similarly sum of all numbers divisible by 5 (sum5) is 5*n(n+1)/2
  23. // Sum of numbers divisible by 15 (sum15) is 15*n(n+1)/2.
  24. long sum3 = 3 * a * (a + 1) / 2;
  25. long sum5 = 5 * b * (b + 1) / 2;
  26. long sum15 = 15 * d * (d + 1) / 2;
  27. long c = sum3 + sum5 - sum15;
  28. System.out.println(c);
  29. }
  30. }
  31. }
Success #stdin #stdout 0.07s 4386816KB
stdin
2
10
100
stdout
23
2318