Python algorithm question

I’m posting my solution to a problem below. Would you mind posting better solutions to the problem below? Also, a clarification: what manner of solution is mine? (In terms of Big O…and why?)

Thank you!

Sorry. Code posted below.

#Create a program that asks the user for a number and then prints out a list 
#of all the divisors of that number. (If you don’t know what a divisor is, 
#it is a number that divides evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)
divisors = []
entry = int(input("Enter a number "))
for n in range(1, entry, 1):
    if entry % n == 0:
        divisors.append(n)
print(divisors)

More condensed way of doing it with list comprehension:

entry = int(input("Enter a number "))
print([n for n in range(1, entry + 1) if entry % n == 0])

An even shorter way of doing it that is just silly:

print([n for n in range(1, int(input("Enter a number ")) + 1) if int(input("Enter a number ")) % n == 0]) 

You have to input the same number for every loop this way but it does work. Definitely NOT a legitimate alternative, just for fun.

EDIT: Code adjusted so that the output includes the entry number itself as a divisor.

I may be wrong as its pretty late, but to find all divisors don’t you only need to progress up to half of the number you entered?

Like if you entered 100, you can stop looping through after 50 because, 50*2 is 100, so there should be no other divisors between 50 and 100. I believe if you want to save more time, you could also save the quotient to your remainder, and then only have to loop up to the sqrt of your number.

Someone please point out if thats just completely wrong… I may just need to go to bed.

Well, that’s only if you store the quotent though, right??? because I figured you’d want 50 to be in your answer set.

yeah, you can loop up to square root, and add divisors to the list in pairs, like that roughly:

function getDivisors(n){
  let result = [];
  let termcondition = Math.sqrt(n);
  for (let i = 1; i < termcondition; i++) {
    if (n % i === 0) {
      result.push(i);
      result.push(n / i);
    }
  }
  // if number has integer square root, should add it to list
  return n % termcondition === 0 ? [...result, termcondition] : result;
}

console.log(getDivisors(100));
// Output:

// [
//   1, 100,  2, 50, 4,
//   25,   5, 20, 10
// ]

Another way. (Trying as suggested above with quotient and count up to half)

number = 100
quotient = number//2
for num in range(1, number//2):
    if number % num == 0:
        divisors.append(num)
if quotient not in divisors:
    divisors.append(quotient)
print(divisors)

Following the javascript example

from math import sqrt
divisors = []
number = 100
term = round(sqrt(number))
for i in range(1,term):
    if number % i == 0:
       divisors.append(number//i)
       divisors.append(i)
divisors.sort()
print(divisors)
1 Like

good stuff, I was trying to do that in python first, since it is python topic
but i forgot too much syntax lol

your solution will not count all divisors for perfect squares like 25, 64, 100 and such

for 100 output is:

# Output:

# [1, 2, 4, 5, 20, 25, 50, 100]

so 10 is missing. You need to add some condition to handle this case

from math import sqrt
divisors = []
number = 100
term = round(sqrt(number))
for i in range(1,term):
    if number % i == 0:
       divisors.append(number//i)
       divisors.append(i)
if term not in divisors:
    divisors.append(term)
divisors.sort()
print(divisors)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.