\[ \let\oldlor\lor \renewcommand{\lor}{\; \oldlor \;} \let\oldland\land \renewcommand{\land}{\; \oldland \;} \renewcommand{\set}[1]{\{\, #1 \,\}} \renewcommand{\given}{\,\mid\,} \renewcommand{\abs}[1]{\lvert #1 \rvert} \renewcommand{\divs}{\!\;\mid\;\!} \renewcommand{\ndivs}{\!\;\nmid\;\!} \renewcommand{\betw}[3][1]{#1 \leq #2 \leq #3} \renewcommand{\mod}[1]{\ (\mathrm{mod}\ #1)} \renewcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \renewcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \renewcommand{\t}[1]{\texttt{#1}} \renewcommand{\fori}[2][i]{\text{for } #1 = 0, 1, \dots, #2} \renewcommand{\x}[1]{\text{#1}} \renewcommand\concat{\mathbin{+\mkern-10mu+}} \DeclareMathOperator*{\CONCAT}{\concat} \DeclareMathOperator*{\SCC}{\|} \]
UVa 543 - Goldbach's Conjecture
Time limit: 3.0 seconds
UVa Difficulty rating: 1
View on UVa

Ah... more prime numbers. In this problem we are asked to verify Goldbach's Conjecture for even numbers, n with 6 <= n <= 1,000,000. Similar to UVa 10140 - Prime Distance, the number of test cases to process is not given in the input. Not always, but often, this style of input is an indicator that some kind of preprocessing step is required to avoid TLE (Time limit exceeded). And also like UVa 10140, that preprocessing step involves computing some primes once and reusing the results for one or more cases to save time.

To accomplish the aforementioned precomputation, we will be using the Sieve of Eratosthenes in tandem with a primality test optimization for large primes. If you have not seen an implementation for Sieve before, an implementation is provided below, and make sure you understand what's going on before continuing.

Once our precomputation is complete, all we need to do for each case is loop over the interval [0, n-1] and test primality for the outermost elements. By outermost, I mean integers i and n - i, whose sum will always give us n for all 0 <= i < n. The output requires we print one line of the form n = a + b, where a and b are odd primes, and if there is more than one pair of odd primes whose sum is n, we must choose a and b such that b - a is maximized.

Since we know n = a + b, and n = i + (n - i), it follows that a = i, and b = n - i, for all i, 0 <= i < n The difference (n - i) - i shrinks as i moves from 0 to n - 1, so we know the first solution found, if one exists, is the desired one. Lastly, we need to print Goldbach's conjecture is wrong, if no solution exists.

Links

C++ References

UVa

230 Borrowers
543 Goldbach's Conjecture
900 Brick Wall Patterns
10047 The Monocycle
10140 Prime Distance
10165 Stone Game
10338 Mischievous Children
10394 Twin Primes
10892 LCM Cardinality

CodeForces

1A Theatre Square
4A Watermelon
25A IQ test
50A Domino piling
58A Chat room
59A Word
69A Young Physicist
71A Way Too Long Words
96A Football
112A Petya and Strings
118A String Task
131A cAPS lOCK
158A Next Round
189A Cut Ribbon
230B T-primes
231A Team
263A Beautiful Matrix
281A Word Capitalization
282A Bit++
318A Even Odds
339A Helpful Maths
339B Xenia and Ringroad
455A Boredom
459B Pashmak and Flowers
474B Worms
479A Expression
486A Calculating Function
489B BerSU Ball
489C Given Length and Sum of Digits...
492B Vanya and Lanterns
500A New Year Transportation
507B Amr and Pins
513A Game
520B Two Buttons
550A Two Substrings
580A Kefa and First Steps
742A Arpa's hard exam and Mehrdad's naive cheat
766B Mahmoud and a Triangle
935A Fafa and his Company
977B Two-gram
977D Divide by three, multiply by two
996A Hit the Lottery
1097A Gennady and a Card Game
1108A Two distinct points
1154A Restoring Three Numbers