2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f39a1000cf542c50fead
|
2018-10-20 18:02:47 +00:00
|
|
|
|
title: 'Problem 46: Goldbach''s other conjecture'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 302134
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='margin-left: 2em;'>
|
|
|
|
|
9 = 7 + 2×1<sup>2</sup><br>
|
|
|
|
|
15 = 7 + 2×2<sup>2</sup><br>
|
|
|
|
|
21 = 3 + 2×3<sup>2</sup><br>
|
|
|
|
|
25 = 7 + 2×3<sup>2</sup><br>
|
|
|
|
|
27 = 19 + 2×2<sup>2</sup><br>
|
|
|
|
|
33 = 31 + 2×1<sup>2</sup>
|
|
|
|
|
</div>
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
It turns out that the conjecture was false.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`goldbachsOtherConjecture()` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof goldbachsOtherConjecture() === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`goldbachsOtherConjecture()` should return 5777.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(goldbachsOtherConjecture(), 5777);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function goldbachsOtherConjecture() {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goldbachsOtherConjecture();
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function goldbachsOtherConjecture() { function isPrime(num) {
|
|
|
|
|
if (num < 2) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (num === 2) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const sqrtOfNum = Math.floor(num ** 0.5);
|
|
|
|
|
for (let i = 2; i <= sqrtOfNum + 1; i++) {
|
|
|
|
|
if (num % i === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isSquare(num) {
|
|
|
|
|
return Math.sqrt(num) % 1 === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// construct a list of prime numbers
|
|
|
|
|
const primes = [];
|
|
|
|
|
for (let i = 2; primes.length < 1000; i++) {
|
|
|
|
|
if (isPrime(i)) primes.push(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let num = 3;
|
|
|
|
|
let answer;
|
|
|
|
|
while (!answer) {
|
|
|
|
|
num += 2;
|
|
|
|
|
if (!isPrime(num)) {
|
|
|
|
|
let found = false;
|
|
|
|
|
for (let primeI = 0; primeI < primes.length && !found; primeI++) {
|
|
|
|
|
const square = (num - primes[primeI]) / 2;
|
|
|
|
|
if (isSquare(square)) {
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) answer = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
```
|