Files
freeCodeCamp/curriculum/challenges/spanish/08-coding-interview-prep/rosetta-code/babbage-problem.spanish.md
2018-10-08 13:34:43 -04:00

2.3 KiB

title, id, localeTitle, challengeType
title id localeTitle challengeType
Babbage problem 594db4d0dedb4c06a2a4cefd 594db4d0dedb4c06a2a4cefd 5

Description

Charles Babbage , mirando el tipo de problemas que su motor analítico podría resolver, dio este ejemplo:

What is the smallest positive integer whose square ends in the digits 269,696?

- Babbage, carta a Lord Bowden, 1837; ver Hollingdale y Tootill, Electronic Computers , segunda edición, 1970, pág. 125.

Pensó que la respuesta podría ser 99,736, cuyo cuadrado es 9,947,269,696; pero no podía estar seguro.

La tarea es averiguar si Babbage tuvo la respuesta correcta.

Implemente una función para devolver el entero más bajo que satisfaga el problema de Babbage. Si Babbage tenía razón, devuelve el número de Babbage.

Instructions

Tests

tests:
  - text: <code>babbage</code> es una función.
    testString: 'assert(typeof babbage === "function", "<code>babbage</code> is a function.");'
  - text: &#39; <code>babbage(99736, 269696)</code> no debe devolver 99736 (hay una respuesta más pequeña).&#39;
    testString: 'assert.equal(babbage(babbageAns, endDigits), answer, "<code>babbage(99736, 269696)</code> should not return 99736 (there is a smaller answer).");'

Challenge Seed

function babbage (babbageNum, endDigits) {
  // Good luck!
  return true;
}

After Test

console.info('after the test');

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;
}