"""Description: Create a function which checks a number for three different properties. is the number prime? is the number even? is the number a multiple of 10? Each should return either true or false, which should be given as an array. Remark: The Haskell variant uses data Property. Examples number_property(7) # ==> [true, false, false] number_property(10) # ==> [false, true, true] The number will always be an integer, either positive or negative. Note that negative numbers cannot be primes, but they can be multiples of 10: number_property(-7) # ==> [false, false, false] number_property(-10) # ==> [false, true, true]""" def isprime(n): y = abs(n) if y == 2: return 1 if y % 2 == 0: return 0 max = y**0.5+1 i = 3 while i <= max: if n % i == 0: return 0 i+=2 return 1 def number_property(n): if n % 2 == 0: if n % 10 == 0: x = [False, True, True] else: x = [False, True, False] else: if isprime(n) == 1: x = [True, False, False] else: x = [False, False, False] if n == 2: x = [True, True, False] else: x = x if n == 1: x = [False, False, False] else: x = x if x < 0: x[0] = False return x number_property(-431)
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