freeCodeCamp/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/babbage-problem.md

2.2 KiB

title id challengeType forumTopicId
Babbage problem 594db4d0dedb4c06a2a4cefd 5 302229

Description

Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example:
What is the smallest positive integer whose square ends in the digits 269,696? Babbage, letter to Lord Bowden, 1837; see Hollingdale and Tootill, Electronic Computers, second edition, 1970, p. 125.
He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain. The task is to find out if Babbage had the right answer.

Instructions

Implement a function to return the lowest integer that satisfies the Babbage problem. If Babbage was right, return Babbage's number.

Tests

tests:
  - text: <code>babbage</code> should be a function.
    testString: assert(typeof babbage === 'function');
  - text: <code>babbage(99736, 269696)</code> should not return 99736 (there is a smaller answer).
    testString: assert.equal(babbage(babbageAns, endDigits), answer);

Challenge Seed

function babbage(babbageNum, endDigits) {

  return true;
}

After Test

const babbageAns = 99736;
const endDigits = 269696;
const answer = 25264;

Solution

function babbage(babbageAns, endDigits) {
  const babbageNum = Math.pow(babbageAns, 2);
  const babbageStartDigits = parseInt(babbageNum.toString().replace('269696', ''));
  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;
}