-3
num = 10000
steps = 0
objective = input("Introduce number of steps")
while steps < int(objective):
    if (num / 3).is_integer():
        num = num - 1
    if num / 3 == 2:
        num = num - 2
    if num / 3 == 1:
        num = num + 2
    steps = steps + 1
print(num)

the problem is that it doesn't output any correct answers

im trying to write a program to solve this problem i found today in a math olympiad:

Clara changes a number N written to the board following the following rule: If N is multiple of 3, then Clara decreases one unit; if the remainder of dividing N by 3 is 2, then Clara decreases 2 units; if the remainder of dividing N by 3 is 1, then Clara increases 2 units. Clara starts with the number 10 000. What number will she have obtained after 2020 steps?

and these are the possible answers:A) 886 B) 6977 C) 6974 D) 6975 E) 919

btw i was supposed to solve these with just paper and pencil so if someone knows how i was supposed to actally do it it'd be interesting

  • In python we have `%`(modulo) operator which returns the remainder after division. You can use that operator for solving this issue. You can read more about the operator at [What is a modulo operator (%) in Python?](https://www.geeksforgeeks.org/what-is-a-modulo-operator-in-python/) – Jishnu Mar 23 '23 at 18:03
  • 1
    You need the modulo operator `%`, not `/`. – Robert Mar 23 '23 at 18:03
  • Welcome to Stack Overflow! Please take the [tour]. Questions on here need to be specific, so this is too broad. The answer is basically, "because the code is wrong" and there are multiple reasons for that. I recommend starting with some simpler programs, like go through a Python tutorial. Specifically, you should learn the difference between if-if and if-elif and [how to test divisibility](/q/8002217/4518341). You should also learn how to use `range()`. – wjandrea Mar 23 '23 at 18:11
  • See also [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341). I realize this isn't homework per se, but a lot of the same points apply, like identifying a specific problem. – wjandrea Mar 23 '23 at 18:14

3 Answers3

0

correct answer is 6976 its needs to be num % whatever. to get the remainder.

num = 10000
steps = 0
objective = input("Introduce number of steps")
while steps < int(objective):
    if (num / 3).is_integer():
        num = num - 1
    elif num % 3 == 2:
        num = num - 2
    elif num % 3 == 1:
        num = num + 2
    steps = steps + 1

print(num)
user540393
  • 145
  • 7
  • 1
    You forgot to replace the first case with `num % 3 == 0`. – chepner Mar 23 '23 at 18:10
  • you don't need to in this case because if is_integer will also catch it. it wouldn't be an interger if it doesnt easily divide. – user540393 Mar 23 '23 at 18:12
  • 1
    My point is, there's no need to divide by three *and* call `is_integer` when you can simply follow the same pattern and check if the remainder is 0. – chepner Mar 23 '23 at 18:20
  • 1
    6976? No, with `objective = 2020`, the answer is 6974. 6976 isn't even one of the options. – wjandrea Mar 23 '23 at 18:23
0

I suggest following changes in the code and you can use a modulo(%) operator. It gives the remainder as its output after division. You can read more about the operator at What is a modulo operator (%) in Python?

num = 10000
steps = 0
objective = input("Introduce number of steps")
while steps < int(objective):
    if  num % 3 == 0: # completely divisible by 3
        num = num - 1
    elif num % 3 == 2: # getting a remainder 2
        num = num - 2 
    elif num % 3 == 1: # getting a remainder 1
        num = num + 2
    steps = steps + 1

print(num)

And the correct comes out to be 6974

Jishnu
  • 151
  • 1
  • 2
  • 12
0

First, you should check the result of num % 3, the remainder when dividing by 3, which can produce either 0 (when num is divisible by 3), 1, or 2.

You have three separate if statements where you should have a single if-elif statement. This inflates the number of steps you actually apply: when num % 3 == 0, you subtract 1, which leaves a number that will have a remainder of 2. But then that's the very next thing you check, which means you'll both subtract 1 and subtract 2 in a single step.

You should use a single if-elif to check for one of three mutually exclusive results:

num = 10000
steps = 0
objective = int(input("Introduce number of steps"))
while steps < objective:
    if num % 3 == 0:
        num = num - 1
    elif num % 3 == 2:
        num = num - 2
    elif num % 3 == 1:
        num = num + 2
    steps = steps + 1
print(num)

You really only need to compute num % 3 once per iteration; you can save the result to check in either an if-elif or separate if statements, since you won't change that result by changing num:

while steps < objective:
    r = num % 3
    if r == 0:
        num -= 1
    elif r == 2:
        num -= 2
    elif r == 1:
        num += 2
    steps += 1

As for how you can proceed on pencil and paper:

If you do this process for a few steps, you'll notice a pattern: you'll add 2 at the first step, but then you'll alternate between subtracting 1 and subtracting 2 "forever", which means you add 2 once, subtract 3 1009 times (i.e., subtract 1 1009 times and subtract 2 1009 times, for a total of 2018 additional steps, bringing us up to 2019 steps total) and subtract 1 on the 2020th and final step.

Then 10000 + 2 - 1009 - 1009*2 - 1 gives us the expected answer of 6974.

chepner
  • 497,756
  • 71
  • 530
  • 681