Project Euler #50: Consecutive prime sum
Question
Answer : 997651
Hacker Rank Problem
Solution (Only 5 test cases passed)
The prime 41, can be written as the sum of six consecutive primes:
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
Answer : 997651
Hacker Rank Problem
Solution (Only 5 test cases passed)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import java.util.Arrays; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int num=Integer.parseInt(sc.nextLine()); for(int i=0;i<num;i++) { int run=sc.nextInt(); System.out.println(run(run)); } } public static String run(int limit) { boolean[] isPrime = listPrimality(limit); int[] primes = listPrimes(limit); long maxSum = 0; int maxRun = -1; for (int i = 0; i < primes.length; i++) { // For each index of a starting prime number int sum = 0; for (int j = i; j < primes.length; j++) { // For each end index (inclusive) sum += primes[j]; if (sum > limit) break; else if (j - i > maxRun && sum > maxSum && isPrime[sum]) { maxSum = sum; maxRun = j - i; } } } return Long.toString(maxSum)+ " " + (maxRun+1); } public static int pow(int x, int y) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Negative exponent"); int z = 1; for (int i = 0; i < y; i++) { if (Integer.MAX_VALUE / z < x) throw new ArithmeticException("Overflow"); z *= x; } return z; } public static boolean[] listPrimality(int n) { if (n < 0) throw new IllegalArgumentException("Negative array size"); boolean[] result = new boolean[n + 1]; if (n >= 2) result[2] = true; for (int i = 3; i <= n; i += 2) result[i] = true; // Sieve of Eratosthenes for (int i = 3, end = sqrt(n); i <= end; i += 2) { if (result[i]) { // Note: i * i does not overflow for (int j = i * i, inc = i * 2; j <= n; j += inc) result[j] = false; } } return result; } public static int[] listPrimes(int n) { boolean[] isPrime = listPrimality(n); int count = 0; for (boolean b : isPrime) { if (b) count++; } int[] result = new int[count]; for (int i = 0, j = 0; i < isPrime.length; i++) { if (isPrime[i]) { result[j] = i; j++; } } return result; } public static int sqrt(int x) { if (x < 0) throw new IllegalArgumentException("Square root of negative number"); int y = 0; for (int i = 1 << 15; i != 0; i >>>= 1) { y |= i; if (y > 46340 || y * y > x) y ^= i; } return y; } } |
Comments
Post a Comment