Project Euler/27: Difference between revisions
From charlesreid1
(Created page with "==Problem Statement== ==Solution== Solution: https://git.charlesreid1.com/cs/euler/src/branch/master/java/Problem027.java Implemented 2 nested for loops to search for param...") |
|||
| Line 1: | Line 1: | ||
==Problem Statement== | ==Problem Statement== | ||
Euler discovered a quadratic formula that would generate primes: | |||
<math> | |||
n^2 + n + 41 | |||
</math> | |||
for <math>0 \leq n \leq 39</math> | |||
Another formula, <math>n^2 - 79n + 1601</math>, was discovered, that produces 80 primes for consecutive values <math>0 \leq n \eq 79</math> | |||
Now consider equadratics of the form | |||
<math> | |||
n^2 + an + b | |||
</math> | |||
where <math>|a| \leq 1000</math>, <math>|b| \leq 1000</math> | |||
Find product of coefficients a, b for the quadratic expression producing the maximum number of primes for consecutive values of n, starting with n = 0 | |||
==Solution== | ==Solution== | ||
Revision as of 12:37, 19 April 2025
Problem Statement
Euler discovered a quadratic formula that would generate primes:
$ n^2 + n + 41 $
for $ 0 \leq n \leq 39 $
Another formula, $ n^2 - 79n + 1601 $, was discovered, that produces 80 primes for consecutive values $ 0 \leq n \eq 79 $
Now consider equadratics of the form
$ n^2 + an + b $
where $ |a| \leq 1000 $, $ |b| \leq 1000 $
Find product of coefficients a, b for the quadratic expression producing the maximum number of primes for consecutive values of n, starting with n = 0
Solution
Solution: https://git.charlesreid1.com/cs/euler/src/branch/master/java/Problem027.java
Implemented 2 nested for loops to search for parameter values by brute force. Keep running track of consecutive number of primes generated, and best a/b. Return them at the end.
This algorithm did not have any trickness, or require any special optimization. Used a standard prime number sieve, nothing special.
Flags