You need to sign in or sign up before continuing.×
5 kyu

Prime number decompositions

322 of 1,586user3482173

Description:

You have to code a function getAllPrimeFactors, which takes an integer as parameter and returns an array containing its prime decomposition by ascending factors. If a factor appears multiple times in the decomposition, it should appear as many times in the array.

exemple: getAllPrimeFactors(100) returns [2,2,5,5] in this order.

This decomposition may not be the most practical.

You should also write getUniquePrimeFactorsWithCount, a function which will return an array containing two arrays: one with prime numbers appearing in the decomposition and the other containing their respective power.

exemple: getUniquePrimeFactorsWithCount(100) returns [[2,5],[2,2]]

You should also write getUniquePrimeFactorsWithProducts, which returns an array containing the prime factors to their respective powers.

exemple: getUniquePrimeFactorsWithProducts(100) returns [4,25]

Errors, if:

  • n is not a number
  • n not an integer
  • n is negative or 0

The three functions should respectively return [], [[],[]] and [].

Edge cases:

  • if n=0, the function should respectively return [], [[],[]] and [].
  • if n=1, the function should respectively return [1], [[1],[1]], [1].
  • if n=2, the function should respectively return [2], [[2],[1]], [2].

The result for n=2 is normal. The result for n=1 is arbitrary and has been chosen to return a usefull result. The result for n=0 is also arbitrary but can not be chosen to be both usefull and intuitive. ([[0],[0]] would be meaningfull but wont work for general use of decomposition, [[0],[1]] would work but is not intuitive.)

Fundamentals

Stats:

CreatedJul 18, 2014
PublishedJul 18, 2014
Warriors Trained7766
Total Skips3575
Total Code Submissions16344
Total Times Completed1586
JavaScript Completions322
Python Completions850
Ruby Completions147
Java Completions307
Total Stars146
% of votes with a positive feedback rating83% of 285
Total "Very Satisfied" Votes204
Total "Somewhat Satisfied" Votes64
Total "Not Satisfied" Votes17
Ad
Contributors
  • user3482173 Avatar
  • jhoffner Avatar
  • GiacomoSorbi Avatar
  • thors Avatar
  • KataSideKick Avatar
  • c-fred Avatar
Ad