I have to create a function that calculates how much factors an integer has. For example, when I call factor(10) the function should be able to tell me it has 4 factors (1, 2, 5, 10). So where would I start off? Would do I need to put?
I have to create a function that calculates how much factors an integer has.
Share
The
%(modulus) operator gives you the remainder of a division. If that remainder is 0, then the second multiple is a factor of the second. So just loop through all the numbers from1tonand check if they’re factors; if so, add them to the list withappend:Here’s a demo.
Or, more concisely using lambdas:
Here’s a demo.