Find Happy numbers
A number h
is happy when iteratively summing the squares of its digits eventually leads 1
.
For example 7
is happy because
7^2 = 49 -> 4^2 + 9^2 = 97 -> 9^2 + 7^2 = 130 -> 1^2 + 3^2 + 0^2 = 10 -> 1^2 + 0^2 = 1.
Similarly, 3
is unhappy, because 3^2 = 9.
Task
Write a function is_happy
returning True
if its integer input h
is happy.
def is_happy(h: int) -> bool:
"""Returns `True` if `h` is happy, `False` otherwise."""
seen = set()
while h > 1 and h not in seen:
seen.add(h)
tot = 0
while h > 0:
tot += pow(h % 10, 2)
h //= 10
h = tot
return h == 1
import codewars_test as test
from solution import is_happy
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(is_happy(3), False)
test.assert_equals(is_happy(4), False)
test.assert_equals(is_happy(7), True)
test.assert_equals(is_happy(19), True)
test.assert_equals(is_happy(103), True)
test.assert_equals(is_happy(487), True)
test.assert_equals(is_happy(1663), True)
test.assert_equals(is_happy(1665), False)
test.assert_equals(is_happy(1000000000), True)
test.assert_equals(is_happy(10000000001), False)
# test.assert_equals(is_happy(), True)
Prime Checker [Optimized]
Write a function prime_checker
taking a single integer input n
and returning True
if n
is prime, False
otherwise.
""" https://en.wikipedia.org/wiki/Primality_test This one has lesser tests or usage of % operator. An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 """ def prime_checker(n): if n in [2, 3, 5]: return True elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0: return False a = int(n ** 0.5 / 30) b = [7, 11, 13, 17, 19, 23, 29, 31] for i in [30 * j for j in range(a)]: if True in [n % (i + q) == 0 for q in b]: return False return True
- """
- https://en.wikipedia.org/wiki/Primality_test
- This one has lesser tests or usage of % operator.
- An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
- """
- def prime_checker(n):
- if n in [2, 3, 5]:
- return True
- elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
- return False
- a = int(n ** 0.5 / 30)
- b = [7, 11, 13, 17, 19, 23, 29, 31]
- for i in [30 * j for j in range(a)]:
- if True in [n % (i + q) == 0 for q in b]:
- return False
return True- return True
import codewars_test as test from solution import prime_checker @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(prime_checker(653), True) test.assert_equals(prime_checker(654), False) test.assert_equals(prime_checker(5), True) test.assert_equals(prime_checker(777), False)# test.assert_equals(prime_checker(977), True) test.assert_equals(prime_checker(125), False)# test.assert_equals(prime_checker(997), True) test.assert_equals(prime_checker(709), True) test.assert_equals(prime_checker(15), False)# test.assert_equals(prime_checker(11), True) test.assert_equals(prime_checker(13), True) test.assert_equals(prime_checker(17), True) test.assert_equals(prime_checker(19), True) test.assert_equals(prime_checker(23), True) test.assert_equals(prime_checker(29), True) test.assert_equals(prime_checker(1021), True) test.assert_equals(prime_checker(553_105_253), True) test.assert_equals(prime_checker(982_451_653), True)
- import codewars_test as test
- from solution import prime_checker
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(prime_checker(653), True)
- test.assert_equals(prime_checker(654), False)
- test.assert_equals(prime_checker(5), True)
- test.assert_equals(prime_checker(777), False)#
- test.assert_equals(prime_checker(977), True)
- test.assert_equals(prime_checker(125), False)#
- test.assert_equals(prime_checker(997), True)
- test.assert_equals(prime_checker(709), True)
- test.assert_equals(prime_checker(15), False)#
- test.assert_equals(prime_checker(11), True)
- test.assert_equals(prime_checker(13), True)
- test.assert_equals(prime_checker(17), True)
- test.assert_equals(prime_checker(19), True)
- test.assert_equals(prime_checker(23), True)
- test.assert_equals(prime_checker(29), True)
- test.assert_equals(prime_checker(1021), True)
- test.assert_equals(prime_checker(553_105_253), True)
- test.assert_equals(prime_checker(982_451_653), True)