-3
w= int(input('enter a num:'))
even= []
for x in range (0, 999999999999999999999999999999999999999999999999, 2):
    even.append(x)
    if w in even:
        print ('the num is even')
        break
else:
    print ('the num is odd')
khelwood
  • 46,621
  • 12
  • 59
  • 83
  • 2
    That loop will take a very long time to execute and produce a list very big. You'd be better off just writing `even = range(0, 999999999999999999999999999999999999999999999999, 2)` which will work instantly and you can immediately check if `w in even` – khelwood Jul 31 '20 at 19:05
  • 1
    Note: you can test if a number is odd with out math operators by testing if the least significant bit is 1 (integers only). – Johnny Mopp Jul 31 '20 at 19:06
  • I appreciate setting a challenge for yourself. For this, what exactly do you consider to be "Math Operators"? I imagine `+`, `-`, `*`, `/`, and `**` are off the table. It looks like you're trying not to use `==`. What about bitwise operators? Any others? – Corey Ogburn Jul 31 '20 at 19:08
  • Even without bitwise operators, it would be simpler to convert the number to a string and test if the last char is in "13579". – Johnny Mopp Jul 31 '20 at 19:12

1 Answers1

1

Though the idea works, that loop would take a very long time to execute and produce a list very big. You'd be better off just using the range object directly.

w= int(input('enter a num:'))
even = range(0, 999999999999999999999999999999999999999999999999, 2)
if w in even:
    print("the number is even")
else:
    print("the number is odd")

which will work instantly (though only for numbers between zero and that big top number). The range object can figure out whether it contains any given number by its own internal arithmetic without having to do any iteration.

(Python 3 only)

khelwood
  • 46,621
  • 12
  • 59
  • 83
  • For someone less familiar with Python's inner workings, how could this possible work instantly? I can see maybe how Python doesn't actually create a list of every even number, but wouldn't Python have to iterate in order to determine if the number was actually in the range? Maybe inputting `2` is fast, but wouldn't `999999999999999999999999999999999999999999999998` take a longer time? – Corey Ogburn Jul 31 '20 at 19:17
  • @CoreyOgburn It figures out whether the range contains any particular number intelligently, using arithmetic based on the endpoints and the step. Checking `999999999999999999999999999999999999999999999998` is just as fast as any other number. – khelwood Jul 31 '20 at 19:18
  • I noticed. I thought about it using endpoints but tests with numbers like `500000000000000000000000000000000000000000000000` (dead center in the middle of the range) still execute seemingly instantly. I might ask my own SO question about this. I'm intrigued. – Corey Ogburn Jul 31 '20 at 19:22
  • 2
    @CoreyOgburn Don't bother: https://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3 – Paul M. Jul 31 '20 at 19:23
  • "only for numbers between zero and that big top number" - Not true, it also works for infinitely many numbers outside that range :-) – superb rain Jul 31 '20 at 19:35
  • 1
    @superbrain Yes, I suppose. "Works" in the way a stopped clock works. – khelwood Jul 31 '20 at 20:02
  • @khelwood can you validate what i asked. I found your answer better but i want to know how to validate my vode. Pls help – Gaurav Chanda Aug 02 '20 at 04:33
  • @GauravChanda I don't understand what else you're asking for. – khelwood Aug 02 '20 at 07:11