Home:ALL Converter>Randomly factor a number into exactly three factors

Randomly factor a number into exactly three factors

Ask Time:2021-03-14T01:53:13         Author:THill3

Json Formatter

I've been over the available questions and answers, and none of them really apply to my situation, so here goes.

I'm working in python and trying to factor the number 1800 into three random factors. I'm not really concerned about the format of the output. I would be okay with a tuple, a list, or even three separate variable numbers. Neither am I concerned about making all of the factors similarly valued. My output could be 2,2,450 or 12,15,10 or anything else.

My best thought was to use the prime factorization (2,2,2,3,3,5,5), create three random subsets, and multiply the items in each subset together. I've looked into multiple methods, and they all appear to have some issues.

ShuffleSplit from scikitlearn will only give me indices of values. So I have to split, get the values into the two lists and then split a second time and put the values into the two new lists. That turns into a lot of lines of code.

Using randint will allow me to find a sublist of prime factors, put that one aside, find another sublist of the remaining prime factors, and then multiply each list out. This is only partially random because I'd have to determine how many factors I want to go into each list.

I could use itertools.permutations (or some other permuting function) to generate a randomly ordered list of the factors and then slice that list manually into three lists. This has the same concern as the randint method in that I have to specify how many small prime factors are included in each of my three output factors.

Listing all possible combinations of factors and randomly choosing one isn't really time feasible.

What's a good (quick, simple, and potentially scalable) way to do this with Python?

Author:THill3,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66616739/randomly-factor-a-number-into-exactly-three-factors
yy