Files
freeCodeCamp/curriculum/challenges/spanish/08-coding-interview-prep/project-euler/problem-3-largest-prime-factor.spanish.md
2018-10-08 13:51:51 -04:00

2.0 KiB

id, localeTitle, challengeType, title
id localeTitle challengeType title
5 5900f36f1000cf542c50fe82 5 Problem 3: Largest prime factor

Description

Los factores primos de 13195 son 5, 7, 13 y 29. ¿Cuál es el factor primo más grande de un number dado?

Instructions

Tests

tests:
  - text: <code>largestPrimeFactor(2)</code> debe devolver 2.
    testString: 'assert.strictEqual(largestPrimeFactor(2), 2, "<code>largestPrimeFactor(2)</code> should return 2.");'
  - text: <code>largestPrimeFactor(3)</code> debe devolver 3.
    testString: 'assert.strictEqual(largestPrimeFactor(3), 3, "<code>largestPrimeFactor(3)</code> should return 3.");'
  - text: <code>largestPrimeFactor(5)</code> debe devolver 5.
    testString: 'assert.strictEqual(largestPrimeFactor(5), 5, "<code>largestPrimeFactor(5)</code> should return 5.");'
  - text: <code>largestPrimeFactor(7)</code> debe devolver 7.
    testString: 'assert.strictEqual(largestPrimeFactor(7), 7, "<code>largestPrimeFactor(7)</code> should return 7.");'
  - text: <code>largestPrimeFactor(13195)</code> debe devolver 29.
    testString: 'assert.strictEqual(largestPrimeFactor(13195), 29, "<code>largestPrimeFactor(13195)</code> should return 29.");'
  - text: <code>largestPrimeFactor(600851475143)</code> debe devolver 6857.
    testString: 'assert.strictEqual(largestPrimeFactor(600851475143), 6857, "<code>largestPrimeFactor(600851475143)</code> should return 6857.");'

Challenge Seed

function largestPrimeFactor(number) {
  // Good luck!
  return true;
}

largestPrimeFactor(13195);

Solution

const largestPrimeFactor = (number)=>{
  let largestFactor = number;
  for(let i = 2;i<largestFactor;i++){
  if(!(largestFactor%i)){
    largestFactor = largestFactor/i;
    largestPrimeFactor(largestFactor);
  }
  }
  return largestFactor;
}