+ 3

Code isn’t working (find all the devidors of input)

I wrote a code in Python that is supposed to find all the devidors of a number, but it doesn’t always work or it skips some lines. I wrote it in suсh a way that it’s more efficiĂ«nt by not itterating over all of the numbers lower than the input. If anyone could tell me what I am doing wrong it would help me a lot. https://code.sololearn.com/cP5AQNp4kZRB/?ref=app

29th Aug 2018, 10:42 PM
Max Poppe
Max Poppe - avatar
8 Respostas
+ 3
check this.in simple way. x=int(input()) for i in range(1,x+1): if(x%i==0): print(i,end=' ')
30th Aug 2018, 8:20 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Maninder Singh yes I know it’s possible to do it that way, but i thought that I would make it more efficiĂ«nt by not checking every number so that large numbers don’t take that much time to find.
30th Aug 2018, 10:48 AM
Max Poppe
Max Poppe - avatar
+ 2
Max Poppe can you tell how you skip the numbers because computer don't know which number should him skip.
30th Aug 2018, 11:05 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Maninder Singh it should skip the even numbers when the number you’re trying to devide is odd and it shouldn’t check numbers as dividers if the number is bigger than half of the number that you’re trying to devide, that’s why i use the x in the function i defined
30th Aug 2018, 11:29 AM
Max Poppe
Max Poppe - avatar
+ 1
Shashi Ranjan I don’t see why you did imported the math module and now now the x doesn’t do anything, could i change that by putting it at the end of the for loop with a comma so it doesn’t try every number and is more efficiĂ«nt?
30th Aug 2018, 10:46 AM
Max Poppe
Max Poppe - avatar
+ 1
Shashi Ranjan yes i know, so I ran it on my desktop using the actual python3.7 and it works perfectly when the input is 9, but when the input is 8 it only gives me the numbers 1 and 2 instead of 1,2,4 and 8.
30th Aug 2018, 11:47 AM
Max Poppe
Max Poppe - avatar
0
Here's the modified version. It's working... import math inp_num = int(input("Type here the number you want to know the dividers from: \n")) def find_devisor(x): devisor = 1 for i in range(1,int(inp_num/2 +1)): # while (devisor <= inp_num): if (inp_num % i == 0): print(i) if inp_num % 2 != 0: find_devisor(2) print(inp_num) else: find_devisor(1) print(inp_num) #input("Press<enter>")
30th Aug 2018, 12:35 AM
йащо Đ Đ°ĐœĐ¶Đ°Đœ
йащо Đ Đ°ĐœĐ¶Đ°Đœ - avatar
0
Max Poppe That math module, sorry forgot to omit it. Actually, I couldn't find any problem with your code. I couldn't figure out what's wrong with your code. Everytime it was just exceeding time limit.
30th Aug 2018, 11:44 AM
йащо Đ Đ°ĐœĐ¶Đ°Đœ
йащо Đ Đ°ĐœĐ¶Đ°Đœ - avatar