<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Project_Euler%2F50</id>
	<title>Project Euler/50 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Project_Euler%2F50"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/50&amp;action=history"/>
	<updated>2026-06-18T23:14:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Project_Euler/50&amp;diff=30597&amp;oldid=prev</id>
		<title>Admin: Automated edit (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/50&amp;diff=30597&amp;oldid=prev"/>
		<updated>2026-06-16T14:34:52Z</updated>

		<summary type="html">&lt;p&gt;Automated edit (via create-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
The prime 41 can be written as the sum of six consecutive primes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;41 = 2 + 3 + 5 + 7 + 11 + 13&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the longest sum of consecutive primes that adds to a prime below one-hundred.&lt;br /&gt;
&lt;br /&gt;
The longest sum of consecutive primes below one-thousand that adds to a prime contains 21 terms and is equal to 953.&lt;br /&gt;
&lt;br /&gt;
Which prime, below one-million, can be written as the sum of the most consecutive primes?&lt;br /&gt;
&lt;br /&gt;
==Approach==&lt;br /&gt;
&lt;br /&gt;
===Sieve of Eratosthenes===&lt;br /&gt;
&lt;br /&gt;
Generate all primes below 1,000,000 using a standard boolean sieve. All numbers from 2 to 999,999 are initially marked as prime, then multiples of each discovered prime are struck off. The resulting &amp;lt;code&amp;gt;isPrime&amp;lt;/code&amp;gt; array provides O(1) primality testing for all integers in range.&lt;br /&gt;
&lt;br /&gt;
===Collect Primes into a List===&lt;br /&gt;
&lt;br /&gt;
Iterate through the &amp;lt;code&amp;gt;isPrime&amp;lt;/code&amp;gt; array, collecting every prime into a compact &amp;lt;code&amp;gt;int[]&amp;lt;/code&amp;gt; array. This allows efficient iteration over only the primes (roughly 78,000 of them below one million), rather than scanning all one million numbers.&lt;br /&gt;
&lt;br /&gt;
===Prefix Sum Array===&lt;br /&gt;
&lt;br /&gt;
Build a prefix sum (cumulative sum) array over the list of primes: &amp;lt;code&amp;gt;prefixSum[k]&amp;lt;/code&amp;gt; stores the sum of the first &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; primes. With this structure, the sum of primes from index &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;j-1&amp;lt;/code&amp;gt; is computed in O(1) time as &amp;lt;code&amp;gt;prefixSum[j] - prefixSum[i]&amp;lt;/code&amp;gt;. This avoids repeatedly summing ranges inside the nested loop.&lt;br /&gt;
&lt;br /&gt;
===Sliding Window Search===&lt;br /&gt;
&lt;br /&gt;
The algorithm uses two nested loops:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Outer loop (i):&amp;#039;&amp;#039;&amp;#039; the starting index of the consecutive prime sequence.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Inner loop (j):&amp;#039;&amp;#039;&amp;#039; the ending index, initialized to &amp;lt;code&amp;gt;i + maxLen + 1&amp;lt;/code&amp;gt; so that only windows strictly longer than the current best are examined. This pruning avoids wasting time on sequences that cannot beat the record.&lt;br /&gt;
&lt;br /&gt;
For each window, compute the sum via the prefix array. If the sum exceeds the one-million limit, break the inner loop (since adding more primes would only increase the sum). If the sum is prime (checked via the &amp;lt;code&amp;gt;isPrime&amp;lt;/code&amp;gt; array in O(1)), update &amp;lt;code&amp;gt;maxLen&amp;lt;/code&amp;gt; and record the prime.&lt;br /&gt;
&lt;br /&gt;
===Return the Result===&lt;br /&gt;
&lt;br /&gt;
After all windows are examined, return the prime that was the sum of the most consecutive primes.&lt;br /&gt;
&lt;br /&gt;
==Solution==&lt;br /&gt;
&lt;br /&gt;
Link: https://git.charlesreid1.com/cs/euler/src/branch/main/java/Problem050.java&lt;br /&gt;
&lt;br /&gt;
==Flags==&lt;br /&gt;
&lt;br /&gt;
{{ProjectEulerFlag}}&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>