Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-46-goldbachs-other-conjecture.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

118 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 5900f39a1000cf542c50fead
challengeType: 5
title: 'Problem 46: Goldbach''s other conjecture'
forumTopicId: 302134
---
## Description
<section id='description'>
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
<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>
It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>goldbachsOtherConjecture()</code> should return a number.
testString: assert(typeof goldbachsOtherConjecture() === 'number');
- text: <code>goldbachsOtherConjecture()</code> should return 5777.
testString: assert.strictEqual(goldbachsOtherConjecture(), 5777);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function goldbachsOtherConjecture() {
// Good luck!
return true;
}
goldbachsOtherConjecture();
```
</div>
</section>
## Solution
<section id='solution'>
```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;
}
```
</section>