fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. public class Main
  8. {
  9. public static void main(String[] args) {
  10. int n = 10000;
  11. int sum = -1;
  12. for (int i = 1; i <= n; i *= 2) {
  13. for (int j = 1; j <= n; j *= 3) {
  14. for (int k = 1; k <= n; k *= 5) {
  15. int x = i*j*k;
  16. if (x <= n) {
  17. sum += x;
  18. } else {
  19. break;
  20. }
  21. }
  22. }
  23. }
  24. System.out.println("sum(constructive)=" + sum);
  25.  
  26. sum = 0;
  27. List<Integer> otherPrimes = new ArrayList<>();
  28. for (int i = 2; i <= n; i++) {
  29. boolean isDivisibleByOtherPrimes = false;
  30. for (int p : otherPrimes) {
  31. if (i % p == 0) {
  32. isDivisibleByOtherPrimes = true;
  33. break;
  34. }
  35. }
  36. if (!isDivisibleByOtherPrimes) {
  37. if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
  38. sum += i;
  39. } else {
  40. otherPrimes.add(i);
  41. }
  42. }
  43. }
  44. System.out.println("sum(DP)=" + sum);
  45. }
  46. }
Success #stdin #stdout 0.12s 35528KB
stdin
Standard input is empty
stdout
sum(constructive)=403028
sum(DP)=403028