Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- def is_prime(n):
- """
- >>> is_prime(2)
- True
- >>> is_prime(3)
- True
- >>> is_prime(4)
- False
- >>> is_prime(20)
- False
- >>> is_prime(21)
- True
- >>> is_prime(43243432)
- False
- """
- if n < 3:
- return True
- elif n % 2 == 0:
- return False
- else:
- return True
- def num_digits(n):
- """
- >>> num_digits(12345)
- 5
- >>> num_digits(0)
- 1
- >>> num_digits(-12345)
- 5
- """
- count = 0
- if n == 0: return 1
- while n:
- count = count + 1
- n = abs(n) / 10
- return count
- def num_even_digits(n):
- """
- >>> num_even_digits(123456)
- 3
- >>> num_even_digits(2468)
- 4
- >>> num_even_digits(1357)
- 0
- >>> num_even_digits(2)
- 1
- >>> num_even_digits(20)
- 2
- """
- count = 0
- while n:
- i = n % 10
- num = (n - i) / 10
- if i % 2 == 0:
- count += 1
- n = num
- return count
- if __name__ == '__main__':
- import doctest
- doctest.testmod()
Add Comment
Please, Sign In to add comment