Project Euler 50 - Consecutive prime sum

Official link: https://projecteuler.net/problem=50

Thought Process

First step with a problem like this is to find an upper bound, notice that the sum of the first 546 primes = 2 + 3 + ... + 3943 = 1001602 which is greater than 10^6, so we are only interested in the first 546 primes.

After this the problem becomes much easier.

I simply do a double nested loop and initialise a list

  1. first loop goes through x from 0 to 546

    • Initialise a total and count

  2. second loop goes through y from x+1 to 546

    • add primes[y] to the total and increase the count, if this total is a prime I append [count, total] to the first list

  3. At the end all I need to do is return the maximum element of the list, which will return [count, total] where count is the longest consecutive run, and total is the prime corresponding to it

Interactive Code

Input an integer (yourinput)

Code will output the [count, total] where total corresponds to the largest prime < yourinput that has the longest run (count) corresponding to it