Guest User

Untitled

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class KPrimes {
  5. public static long[] countKprimes (int k, long start, long end) {
  6. List<Long> result = new ArrayList<>();
  7.  
  8. for (long i = start == 0 ? 2 : start; i <= end; i++) {
  9. int numOfFactors = 0;
  10. long cur = i;
  11.  
  12. for (int j = 2; j <= cur / j; j++) {
  13. while (cur % j == 0) {
  14. numOfFactors++;
  15. cur /= j;
  16. }
  17. }
  18.  
  19. if (cur > 1) {
  20. numOfFactors++;
  21. }
  22.  
  23. if (numOfFactors == k) result.add(i);
  24. }
  25.  
  26. return result.stream().mapToLong(l -> l).toArray();
  27. }
  28.  
  29. public static int puzzle (int s) {
  30. int combinations = 0;
  31.  
  32. long[] onePrimes = countKprimes(1, 2, s);
  33. long[] threePrimes = countKprimes(3, 2, s);
  34. long[] sevenPrimes = countKprimes(7, 2, s);
  35.  
  36. for (long i : onePrimes) {
  37. for (long j : threePrimes) {
  38. for (long k : sevenPrimes) {
  39. if (i + j + k == s) combinations++;
  40. }
  41. }
  42. }
  43.  
  44. return combinations;
  45. }
  46. }
Add Comment
Please, Sign In to add comment