简体   繁体   中英

how do i get all the divisors of an input using its prime factors and multiplicities?

i've been trying to solve this problem for days but i still couldnt get the correct output. the problem is like this, for example if i use the number 50, it has a list [2, 5] as its prime factors and the corresponding multiplicities of those factors are as follows [1, 2]

prime_factors = [2, 5]
multiplicities = [1, 2]

so the divisors should have an output of [1, 2, 5, 10, 25, 50] but that's just the problem, how do i create a function that gets all the divisors of a certain input , using its prime factors and their corresponding multiplicities ?

Well, the question is "how do I write a function", so maybe those advices may help you:

  1. Get simple example . You already did this part - you have input 50 (prime_factors = [2, 5] multiplicities = [1, 2]) and the output of [1, 2, 5, 10, 25, 50].
  2. Try to work example on paper step by step . - Just try to figure out how the output is obtained from input. For example, 10 in the output list comes from 5 in prime_factors to the power of 1 from multiplicities multiplied by 2 to the power of 1 from multiplicities. Try to figure out the rest of elements in the output list. Where does the 25 come from?
  3. Come up with the function signature . - Think about what is the minimal information you need to figure out the result and what form should the result have? That should be easy since you already posted and example with well-defined inputs and output.
  4. Try to generify the steps you did with example. - When you will come up with solution for your example, try to think about the other one. How would you get the result for 40 or 60? Can you use the same patterns as for 50?
  5. Write some pseudocode . - Write your idea in a code-like fashion. Do some loops if necessary, name variables, structure your code a little. That should result in the result of 4th step, but ready to be written in code.
  6. Write the code . - Since you want to program in Python, change your pseudocode to the Python one. This step may be difficult depending on your skills, but if you come this far, people here will help you with finishing.

If you are stuck on any of these steps, people here will gladly help, we just have to have some starting point. I would recommend writing out each element in the output and where it comes from. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM