I have an animation-related problem. To quickly find number of frames that will loop perfectly within animation sequence, I need to know what round divisions of total number of frames are.
I found a code that finds all round divisors of a number, and it's doing it's job, but I also need to sort it by value.
import math
# method to print the divisors
def printDivisors(n) :
# Note that this loop runs till square root
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
# If divisors are equal, print only one
if (n / i == i) :
print (i,end=" ")
else :
# Otherwise print both
print (i , int(n/i), end=" ")
i = i 1
# Driver method
frames=int(input("Enter a number of frames you have: "))
print ("The divisors for the number of frames you have: ")
printDivisors(frames)
#Original code is contributed by Nikita Tiwari.
To sort results I tried to use list and sort functions, but seems like I don't know what I'm doing, because list seems to be not updated.
import math
buffer_list = []
# method to print the divisors
def printDivisors(n) :
# Note that this loop runs till square root
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
# If divisors are equal, append to list only one
if (n / i == i) :
buffer_list.append(i)
else :
# Otherwise append to list both
buffer_list.append(i , int(n/i))
i = i 1
# Driver method
frames=int(input("Enter a number of frames you have: "))
print ("The divisors for the number of frames you have: ")
# Sort and print list
buffer_list.sort()
print (buffer_list)
#Original code is contributed by Nikita Tiwari.
CodePudding user response:
You are forgetting to call the printDivisors
function, so no items are being added to your list. The sorting code itself is fine.
I've fixed your code below. I changed printDivisors
to calculateDivisors
, as it is not actually printing anything. I also fixed the second buffer_list.append
call, as append
only takes one argument.
import math
buffer_list = []
def calculateDivisors(n):
i = 1
while i <= math.sqrt(n):
if (n % i == 0):
if (n / i == i):
buffer_list.append(i)
else:
buffer_list.append(n // i)
i = i 1
frames = int(input("Enter a number of frames you have: "))
calculateDivisors(frames)
print("The divisors for the number of frames you have: ")
buffer_list.sort()
print(buffer_list)
CodePudding user response:
- Instead of modifying a global list, return it from your function.
- Instead of a
while
loop, use afor
loop. list.append
only takes a single argument.- Instead of
int(n / i)
use integer division:n // i
- You don't need to sort the list, you already know that
i
is increasing andn // i
is decreasing. You just need to make sure that the latter are reversed. - I agree with Jack Taylor that you want to change the name of the function.
- I'll add type annotations for clarity.
- You don't need to check for
n // i == i
every iteration, since it can only happen at the last iteration.
Here is a possible solution:
import math
def divisors(n: int) -> list[int]:
small_divisors = []
large_divisors = []
sqrt_n = int(math.sqrt(n))
for i in range(1, sqrt_n):
if n % i == 0:
small_divisors.append(i)
large_divisors.append(n // i)
if n % sqrt_n == 0:
small_divisors.append(sqrt_n)
return small_divisors large_divisors[::-1]
frames = int(input("Enter a number of frames you have: "))
print("The divisors for the number of frames you have:")
print(divisors(frames))