+ 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
8 Answers
+ 3
check this.in simple way.
x=int(input())
for i in range(1,x+1):
if(x%i==0):
print(i,end=' ')
+ 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.
+ 2
Max Poppe can you tell how you skip the numbers because computer don't know which number should him skip.
+ 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
+ 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?
+ 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.
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>")
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.