2018-09-30 22:01:58 +00:00
---
id: 594db4d0dedb4c06a2a4cefd
2020-11-27 18:02:05 +00:00
title: Babbage problem
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302229
2021-01-13 02:31:00 +00:00
dashedName: babbage-problem
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
[Charles Babbage ](https://en.wikipedia.org/wiki/Charles_Babbage "wp: Charles_Babbage" ), looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example:
2019-02-25 04:36:09 +00:00
< blockquote >
What is the smallest positive integer whose square ends in the digits 269,696?
2020-11-27 18:02:05 +00:00
< footer style = 'margin-left: 2em;' > Babbage, letter to Lord Bowden, 1837; see Hollingdale and Tootill, < i > Electronic Computers< / i > , second edition, 1970, p. 125.< / footer >
2019-02-25 04:36:09 +00:00
< / blockquote >
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
The task is to find out if Babbage had the right answer.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
2019-03-01 08:10:50 +00:00
Implement a function to return the lowest integer that satisfies the Babbage problem. If Babbage was right, return Babbage's number.
2018-09-30 22:01:58 +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
`babbage` should be a function.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof babbage === 'function');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`babbage(99736, 269696)` should not return 99736 (there is a smaller answer).
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.equal(babbage(babbageAns, endDigits), answer);
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
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2018-10-20 18:02:47 +00:00
const babbageAns = 99736;
const endDigits = 269696;
const answer = 25264;
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
2020-11-27 18:02:05 +00:00
```js
function babbage(babbageNum, endDigits) {
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
return true;
}
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2019-02-26 08:07:07 +00:00
function babbage(babbageAns, endDigits) {
2018-09-30 22:01:58 +00:00
const babbageNum = Math.pow(babbageAns, 2);
2018-10-20 18:02:47 +00:00
const babbageStartDigits = parseInt(babbageNum.toString().replace('269696', ''));
2018-09-30 22:01:58 +00:00
let answer = 99736;
// count down from this answer and save any sqrt int result. return lowest one
for (let i = babbageStartDigits; i >= 0; i--) {
const num = parseInt(i.toString().concat('269696'));
const result = Math.sqrt(num);
if (result === Math.floor(Math.sqrt(num))) {
answer = result;
}
}
return answer;
}
```