Difficulty understanding calculation errors [closed]
up vote
-1
down vote
favorite
I have written some code, but it doesn't get the right results all the times
and I can't understand what is the problem.
For example, I changed for to be in range(2) and give it 570 and 594, the right answer is 594 16 but it gives me 570 16 but in some other cases it will work properly and I don't know what is the problem!
maxim=0
maxnum=0
def divisor(number):
counter=0
for i in range(0,number):
if number%(i+1) == 0:
counter+=1
return counter
for i in range(20):
num=int(input())
divis=divisor(num)
if maxim<divis:
maxnum=num
maxim=divis
maxnum=num
print(maxnum,maxim)
python
closed as unclear what you're asking by tripleee, Vega, sideshowbarker, AdrianHHH, ewolden Nov 9 at 10:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I have written some code, but it doesn't get the right results all the times
and I can't understand what is the problem.
For example, I changed for to be in range(2) and give it 570 and 594, the right answer is 594 16 but it gives me 570 16 but in some other cases it will work properly and I don't know what is the problem!
maxim=0
maxnum=0
def divisor(number):
counter=0
for i in range(0,number):
if number%(i+1) == 0:
counter+=1
return counter
for i in range(20):
num=int(input())
divis=divisor(num)
if maxim<divis:
maxnum=num
maxim=divis
maxnum=num
print(maxnum,maxim)
python
closed as unclear what you're asking by tripleee, Vega, sideshowbarker, AdrianHHH, ewolden Nov 9 at 10:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
4
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
what ismaxnum
andmaxim
suppose to be?
– Andreas
Nov 9 at 8:11
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have written some code, but it doesn't get the right results all the times
and I can't understand what is the problem.
For example, I changed for to be in range(2) and give it 570 and 594, the right answer is 594 16 but it gives me 570 16 but in some other cases it will work properly and I don't know what is the problem!
maxim=0
maxnum=0
def divisor(number):
counter=0
for i in range(0,number):
if number%(i+1) == 0:
counter+=1
return counter
for i in range(20):
num=int(input())
divis=divisor(num)
if maxim<divis:
maxnum=num
maxim=divis
maxnum=num
print(maxnum,maxim)
python
I have written some code, but it doesn't get the right results all the times
and I can't understand what is the problem.
For example, I changed for to be in range(2) and give it 570 and 594, the right answer is 594 16 but it gives me 570 16 but in some other cases it will work properly and I don't know what is the problem!
maxim=0
maxnum=0
def divisor(number):
counter=0
for i in range(0,number):
if number%(i+1) == 0:
counter+=1
return counter
for i in range(20):
num=int(input())
divis=divisor(num)
if maxim<divis:
maxnum=num
maxim=divis
maxnum=num
print(maxnum,maxim)
python
python
edited Nov 9 at 8:09
Flimzy
36.1k96496
36.1k96496
asked Nov 9 at 8:05
kosar afrooshe
72
72
closed as unclear what you're asking by tripleee, Vega, sideshowbarker, AdrianHHH, ewolden Nov 9 at 10:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by tripleee, Vega, sideshowbarker, AdrianHHH, ewolden Nov 9 at 10:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
4
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
what ismaxnum
andmaxim
suppose to be?
– Andreas
Nov 9 at 8:11
add a comment |
4
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
what ismaxnum
andmaxim
suppose to be?
– Andreas
Nov 9 at 8:11
4
4
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
what is
maxnum
and maxim
suppose to be?– Andreas
Nov 9 at 8:11
what is
maxnum
and maxim
suppose to be?– Andreas
Nov 9 at 8:11
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
both 594 and 570 has 16 divisors each. You are changing the maxnum
only if the number of divisors is greater than the current maxim
. So since 570 comes as the first input maxnum
will be 570. change like this,
if maxim<=divis:
if (maxim==divis and num>maxnum) or maxim<divis:
maxnum=num
maxim=divis
add a comment |
up vote
0
down vote
your maxnum update condition is maxim<divis
. 570
and 594
got same divis
, so maxnum
is 570
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
both 594 and 570 has 16 divisors each. You are changing the maxnum
only if the number of divisors is greater than the current maxim
. So since 570 comes as the first input maxnum
will be 570. change like this,
if maxim<=divis:
if (maxim==divis and num>maxnum) or maxim<divis:
maxnum=num
maxim=divis
add a comment |
up vote
3
down vote
both 594 and 570 has 16 divisors each. You are changing the maxnum
only if the number of divisors is greater than the current maxim
. So since 570 comes as the first input maxnum
will be 570. change like this,
if maxim<=divis:
if (maxim==divis and num>maxnum) or maxim<divis:
maxnum=num
maxim=divis
add a comment |
up vote
3
down vote
up vote
3
down vote
both 594 and 570 has 16 divisors each. You are changing the maxnum
only if the number of divisors is greater than the current maxim
. So since 570 comes as the first input maxnum
will be 570. change like this,
if maxim<=divis:
if (maxim==divis and num>maxnum) or maxim<divis:
maxnum=num
maxim=divis
both 594 and 570 has 16 divisors each. You are changing the maxnum
only if the number of divisors is greater than the current maxim
. So since 570 comes as the first input maxnum
will be 570. change like this,
if maxim<=divis:
if (maxim==divis and num>maxnum) or maxim<divis:
maxnum=num
maxim=divis
answered Nov 9 at 8:20
Gautham M
15218
15218
add a comment |
add a comment |
up vote
0
down vote
your maxnum update condition is maxim<divis
. 570
and 594
got same divis
, so maxnum
is 570
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
add a comment |
up vote
0
down vote
your maxnum update condition is maxim<divis
. 570
and 594
got same divis
, so maxnum
is 570
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
add a comment |
up vote
0
down vote
up vote
0
down vote
your maxnum update condition is maxim<divis
. 570
and 594
got same divis
, so maxnum
is 570
your maxnum update condition is maxim<divis
. 570
and 594
got same divis
, so maxnum
is 570
edited Nov 9 at 8:39
Blaze
2,8661524
2,8661524
answered Nov 9 at 8:16
cuble
14617
14617
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
add a comment |
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
That would appear to be the desired behaviour, to find the number with the maximum number of factors. (But if two numbers have the same max number of factors, the OP needs to pick the biggest, but that's a slightly separate matter)
– Ken Y-N
Nov 9 at 8:17
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
the condition "maxim<divis" maybe not what he want. Sorry may the incomplete post.
– cuble
Nov 9 at 8:20
add a comment |
4
Litter your code with print functions, or properly step through it with a debugger, and decipher where the logic in your code is wrong. ericlippert.com/2014/03/05/how-to-debug-small-programs is a good read for that as well.
– 9769953
Nov 9 at 8:10
what is
maxnum
andmaxim
suppose to be?– Andreas
Nov 9 at 8:11