def digit(number, order): return number % (10 ** order) / (10 ** (order - 1)) def is_magic(number): half1sum = digit(number, 6) + digit(number, 5) + digit(number, 4) half2sum = digit(number, 3) + digit(number, 2) + digit(number, 1) return half1sum == half2sum number1 = 917173 number2 = 918273 magic_count = 0 for number in xrange(number1, number2 + 1): if is_magic(number): print "{} is magic number!".format(number) magic_count += 1 print "{} magic numbers in the range from {} to {}".format(magic_count, number1, number2)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question