Consider the prime number `23`. If we sum the square of its digits we get:
`2^2 + 3^2 = 13`, then for `13: 1^2 + 3^2 = 10`, and finally for `10: 1^2 + 0^2 = 1`. 

Similarly, if we start with prime number `7`, the sequence is: `7->49->97->130->10->1`.

Given a range, how many primes within that range will eventually end up being `1`? 

The upperbound for the range is `50,000`. A range of `(2,25)` means that: `2 <= n < 25`. 

Good luck!

If you like this Kata, please try:

[Prime reversion](https://www.codewars.com/kata/59b46276afcda204ed000094)

[Domainant primes](https://www.codewars.com/kata/59ce11ea9f0cbc8a390000ed)

Difficulty: introductory

Starter code:

def solve(a,b):
	

Solutions:
def is_prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    sqrtn = int(n**0.5) + 1
    for i in range(5, sqrtn, 6):
        if n % i == 0 or n % (i + 2) == 0:
            return False
    return True

def end_one(n):
    while n > 6:
        n = sum(map(lambda x: int(x)*int(x) ,f"{n}"))
        if n == 1:
            return True
        
def solve(a,b):
    return sum(1 for n in range(a, b) if is_prime(n) and end_one(n))

def solve(a,b):
    return len([x for x in [7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487, 563, 617, 653, 673, 683, 709, 739, 761, 863, 881, 907, 937, 1009, 1033, 1039, 1093, 1151, 1277, 1303, 1373, 1427, 1447, 1481, 1487, 1511, 1607, 1663, 1697, 1733, 1847, 1933, 2003, 2039, 2063, 2111, 2221, 2309, 2333, 2339, 2383, 2393, 2417, 2557, 2693, 2741, 2833, 2851, 2903, 2963, 3001, 3019, 3067, 3079, 3083, 3109, 3137, 3209, 3301, 3313, 3319, 3323, 3329, 3331, 3371, 3391, 3463, 3607, 3637, 3643, 3673, 3709, 3779, 3797, 3803, 3823, 3833, 3907, 3923, 3931, 4111, 4127, 4157, 4217, 4271, 4363, 4441, 4447, 4481, 4517, 4663, 4721, 4751, 4817, 4871, 5147, 5227, 5281, 5417, 5471, 5477, 5527, 5569, 5659, 5741, 5821, 5879, 5897, 5987, 6037, 6053, 6073, 6163, 6197, 6203, 6329, 6337, 6343, 6353, 6361, 6367, 6373, 6389, 6637, 6661, 6673, 6701, 6703, 6719, 6733, 6763, 6791, 6803, 6899, 6917, 6971, 6983, 7039, 7127, 7309, 7331, 7451, 7457, 7481, 7541, 7547, 7589, 7603, 7691, 7793, 7841, 7937, 8081, 8147, 8233, 8369, 8521, 8597, 8693, 8699, 8741, 8821, 8929, 8963, 8969, 9001, 9007, 9013, 9103, 9133, 9203, 9323, 9377, 9587, 9623, 9689, 9829, 9857, 10009, 10039, 10067, 10093, 10141, 10151, 10177, 10211, 10247, 10303, 10333, 10337, 10427, 10457, 10487, 10607, 10663, 10733, 10771, 10847, 10903, 11113, 11131, 11149, 11159, 11197, 11243, 11251, 11311, 11423, 11467, 11471, 11483, 11491, 11519, 11681, 11719, 11941, 11971, 12011, 12101, 12143, 12149, 12347, 12413, 12437, 12473, 12491, 12511, 12583, 12671, 12743, 12823, 12841, 12853, 12941, 12959, 13003, 13009, 13033, 13037, 13093, 13177, 13241, 13309, 13339, 13411, 13421, 13457, 13487, 13499, 13577, 13679, 13697, 13757, 13841, 13903, 13933, 13967, 14011, 14057, 14081, 14087, 14207, 14281, 14321, 14327, 14347, 14387, 14407, 14437, 14449, 14461, 14537, 14549, 14551, 14561, 14723, 14753, 14783, 14813, 14821, 14831, 14939, 15101, 15121, 15299, 15377, 15451, 15461, 15473, 15541, 15641, 15679, 15737, 15773, 15787, 15823, 15877, 15887, 16007, 16063, 16097, 16127, 16217, 16363, 16417, 16451, 16603, 16633, 16741, 16759, 16811, 16937, 16993, 17027, 17033, 17107, 17137, 17191, 17207, 17317, 17443, 17483, 17569, 17573, 17609, 17627, 17659, 17669, 17713, 17827, 17911, 18041, 18047, 18143, 18223, 18253, 18341, 18401, 18413, 18523, 18587, 18743, 18757, 18839, 18899, 19141, 19259, 19333, 19421, 19699, 19763, 19889, 19963, 19979, 19997, 20063, 20147, 20177, 20333, 20369, 20393, 20639, 20693, 20717, 20771, 20899, 20903, 20963, 21011, 21101, 21143, 21149, 21283, 21341, 21347, 21407, 21419, 21481, 21491, 21599, 21617, 21767, 21787, 21841, 22129, 22229, 22273, 22291, 22381, 22483, 22549, 22573, 22621, 22651, 22783, 22861, 22921, 23039, 23227, 23369, 23417, 23447, 23581, 23609, 23663, 23741, 23827, 23887, 23893, 23899, 23977, 24071, 24107, 24113, 24137, 24181, 24229, 24317, 24371, 24473, 24683, 24859, 25057, 25111, 25183, 25237, 25261, 25453, 25561, 25621, 25657, 25801, 25849, 25919, 26003, 26171, 26177, 26227, 26251, 26267, 26309, 26339, 26393, 26557, 26627, 26633, 26669, 26711, 26717, 26821, 26903, 27017, 27107, 27143, 27253, 27283, 27299, 27397, 27431, 27611, 27617, 27697, 27701, 27739, 27793, 27817, 27823, 27883, 27967, 28051, 28081, 28099, 28123, 28351, 28387, 28393, 28411, 28463, 28513, 28549, 28621, 28643, 28723, 28771, 28789, 28837, 28879, 28909, 28933, 29033, 29063, 29221, 29297, 29303, 29363, 29383, 29389, 29411, 29633, 29833, 29927, 29983, 30013, 30029, 30091, 30097, 30103, 30109, 30133, 30137, 30139, 30269, 30293, 30313, 30319, 30323, 30367, 30391, 30553, 30637, 30643, 30661, 30689, 30713, 30763, 30803, 30869, 30931, 30977, 31033, 31039, 31177, 31247, 31307, 31393, 31481, 31547, 31663, 31699, 31769, 31771, 31847, 32009, 32069, 32083, 32141, 32257, 32303, 32309, 32369, 32411, 32609, 32693, 32779, 32797, 32803, 32839, 32887, 32983, 33013, 33023, 33029, 33071, 33083, 33091, 33107, 33203, 33289, 33301, 33359, 33391, 33413, 33589, 33629, 33829, 33931, 34127, 34147, 34157, 34211, 34217, 34313, 34471, 34499, 34603, 34721, 34747, 34781, 34871, 34897, 34919, 34949, 35053, 35227, 35281, 35339, 35393, 35569, 35603, 35771, 35839, 35933, 35983, 36007, 36037, 36061, 36067, 36073, 36209, 36263, 36293, 36307, 36343, 36433, 36559, 36607, 36637, 36791, 36809, 36919, 36923, 37013, 37097, 37117, 37171, 37441, 37447, 37489, 37517, 37571, 37619, 37663, 37691, 37907, 38069, 38189, 38239, 38287, 38299, 38303, 38329, 38333, 38593, 38609, 38669, 38749, 38891, 38923, 38953, 39023, 39103, 39133, 39301, 39313, 39419, 39619, 39623, 39671, 39727, 39749, 39761, 39799, 39829, 39847, 39979, 40009, 40087, 40111, 40127, 40471, 40577, 40609, 40751, 40841, 41011, 41047, 41057, 41081, 41113, 41117, 41131, 41183, 41213, 41231, 41281, 41333, 41357, 41381, 41387, 41399, 41507, 41549, 41617, 41641, 41651, 41761, 41801, 41813, 41911, 42017, 42071, 42131, 42181, 42283, 42437, 42473, 42589, 42683, 42701, 42743, 42859, 42863, 43063, 43133, 43271, 43313, 43331, 43427, 43499, 43517, 43633, 43721, 43781, 43789, 43987, 43991, 43997, 44017, 44041, 44071, 44159, 44249, 44273, 44371, 44453, 44491, 44519, 44543, 44633, 44647, 44651, 44699, 44701, 44773, 44789, 44879, 44939, 44959, 44987, 45077, 45137, 45161, 45289, 45317, 45491, 45523, 45553, 45641, 45707, 45853, 45949, 46141, 46171, 46411, 46447, 46451, 46499, 46511, 46663, 46997, 47041, 47051, 47057, 47111, 47123, 47143, 47161, 47351, 47381, 47389, 47407, 47431, 47501, 47507, 47513, 47699, 47743, 47857, 47939, 47969, 48017, 48121, 48131, 48259, 48311, 48371, 48397, 48449, 48479, 48497, 48623, 48731, 48757, 48799, 48947, 48973, 49121, 49139, 49193, 49211, 49391, 49451, 49459, 49549, 49697, 49739, 49783, 49789, 49937, 49943] if x in range(a,b)])


import math as m
def IsPrime(a):
    if a==1:
        return False
    for i in range(2,int(m.sqrt(a)+1)):
        if a%i==0:
            return False
    return True
def algorithm(a):
    while a!=4:
        sum=0
        for j in str(a):
            sum+=int(j)**2
        a=sum
        if a==1:
            return True
    return False   
def solve(a,b):
    counter=0
    for i in range(a,b):
        if IsPrime(i):
            if algorithm(i):
                counter+=1
    return counter


def build_solver(limit):
    # Initial list of known values
    reducers = [(2, 0), (3, 0), (5, 0)]
    # Intermediate values known to end in a non-terminating sequence
    known_failures = {2, 3, 5}
    
    def reduces_to_1(p):
        # Determine if successive summation of digit squares reduces to 1
        seq = set()
        n = p
        while n not in seq and n not in known_failures and n != 1:
            seq.add(n)
            m = n
            t = 0
            while m > 0:
                m, d = divmod(m, 10)
                t += d * d
            n = t
        if n != 1:
            # Add to the set of known failing intermeduiate states
            known_failures.update(seq)
        return n == 1
    
    # Build primes up to limit
    prime_candidate = reducers[-1][0]
    root = int(prime_candidate ** 0.5) + 1
    while prime_candidate < limit:
        prime_candidate += 2
        if root * root < prime_candidate:
            root += 1
        for p, _ in reducers:
            if prime_candidate % p == 0:
                break
            if p > root:
                reducers.append((prime_candidate, reduces_to_1(prime_candidate)))
                break
    
    # Keep only those primes that reduce to 1
    reducers = [p for p, r in reducers if r]

    def solve(a, b):
        result = 0
        for p in reducers:
            if p < a:
                continue
            if p >= b:
                break
            result += 1
        return result
    
    return solve
        
solve = build_solver(50_000)

import numpy as np

def ssloop(n):
    seen = set()
    while n != 1 and n not in seen:
        seen.add(n)
        n = sum(x*x for x in map(int, str(n)))
    return n == 1

s = np.ones(50001)
s[:2] = s[4::2] = 0
for i in range(3, int(len(s) ** 0.5) + 1, 2):
    if s[i]:
        s[i*i::i] = 0
xs = [i for i, x in enumerate(s) if x and ssloop(i)]

def solve(a,b):
    return np.searchsorted(xs, b) - np.searchsorted(xs, a)

N = 10 ** 5

def sum_square_digits(n):
    return sum((ord(d) - 48) ** 2 for d in str(n))

def reductible(n):
    suite = set()
    while n not in suite:
        suite.add(n)
        n = sum_square_digits(n)
    return n == 1

max_digit_sum = 81 * len(str(N))
sum_reduc = set(filter(reductible, range(max_digit_sum + 1)))

def primes(n):
    sieve = n // 2 * [True]
    for i in range(3, int(n**.5) + 1, 2):
        if sieve[i // 2]:
            sieve[i*i // 2::i] = [False] * ((n - i*i - 1) // (2*i) + 1)
    return [2] + [2*i + 1 for i in range(1, n // 2) if sieve[i]]

prime_reduc = [n for n in primes(N) if sum_square_digits(n) in sum_reduc]

from bisect import bisect_left
def solve(a, b):
    return bisect_left(prime_reduc, b) - bisect_left(prime_reduc, a)

def isprime(n):
    if n<2: return False
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True
    
def solve(a,b):
    count = 0
    for i in range(a,b):
        if isprime(i):
            c = 0
            while i!=1 and c!= 10:
                i = sum(int(j)**2 for j in list(str(i)))
                c += 1
            if i==1:
                count += 1
    return count

N = 50000
sieve, reduceable = [False, False] + [True] * (N - 1), set()
for i, is_prime in enumerate(sieve):
    if is_prime:
        seen, n = set(), i
        while n not in seen:
            seen.add(n)
            n = sum(int(c)**2 for c in str(n))
            if n == 1: reduceable.add(i)            

        for j in range(i*i, N+1, i): sieve[j] = False

def solve(a,b):
    return sum(1 for e in reduceable if a <= e < b)