| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5900f36f1000cf542c50fe82 | 
					
						
							|  |  |  | challengeType: 5 | 
					
						
							|  |  |  | title: 'Problem 3: Largest prime factor' | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Description
 | 
					
						
							|  |  |  | <section id='description'> | 
					
						
							|  |  |  | The prime factors of 13195 are 5, 7, 13 and 29. | 
					
						
							|  |  |  | What is the largest prime factor of the given <code>number</code>? | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Instructions
 | 
					
						
							|  |  |  | <section id='instructions'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Tests
 | 
					
						
							|  |  |  | <section id='tests'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```yml | 
					
						
							|  |  |  | tests: | 
					
						
							|  |  |  |   - text: <code>largestPrimeFactor(2)</code> should return 2. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(2), 2, '<code>largestPrimeFactor(2)</code> should return 2.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>largestPrimeFactor(3)</code> should return 3. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(3), 3, '<code>largestPrimeFactor(3)</code> should return 3.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>largestPrimeFactor(5)</code> should return 5. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(5), 5, '<code>largestPrimeFactor(5)</code> should return 5.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>largestPrimeFactor(7)</code> should return 7. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(7), 7, '<code>largestPrimeFactor(7)</code> should return 7.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>largestPrimeFactor(13195)</code> should return 29. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(13195), 29, '<code>largestPrimeFactor(13195)</code> should return 29.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>largestPrimeFactor(600851475143)</code> should return 6857. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(largestPrimeFactor(600851475143), 6857, '<code>largestPrimeFactor(600851475143)</code> should return 6857.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Challenge Seed
 | 
					
						
							|  |  |  | <section id='challengeSeed'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div id='js-seed'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function largestPrimeFactor(number) { | 
					
						
							|  |  |  |   // Good luck! | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | largestPrimeFactor(13195); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Solution
 | 
					
						
							|  |  |  | <section id='solution'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const largestPrimeFactor = (number)=>{ | 
					
						
							|  |  |  |   let largestFactor = number; | 
					
						
							|  |  |  |   for(let i = 2;i<largestFactor;i++){ | 
					
						
							|  |  |  |   if(!(largestFactor%i)){ | 
					
						
							|  |  |  |     largestFactor = largestFactor/i; | 
					
						
							|  |  |  |     largestPrimeFactor(largestFactor); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return largestFactor; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> |