So I have this problem involving Super B primes. Super B prime is basically this.
- If n- is prime
- And sum of digits in n is prime
- And sum of digits in binary(n) is prime
Then n is Super B prime. E.g.
- 41-is prime
- 4+1=5 -is prime
- 41(10)=101001(2) is 1+0+1+0+0+1=3 -is prime
=======
So 41 is Super B prime.
The problem is that I have to print every Super B Prime in range(a,b) and what I have now exceeds the time limit by alot. How can I improve it?
import math
from functools import lru_cache
@lru_cache()
def is_prime(n):
if n<1:
raise ValueError("Must be positive integer")
elif n>1:
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
@lru_cache()
def dec_to_binary(n):
return int(bin(n)[2:])
a,b = map(int,input().split())
for i in range(a, b+1):
l=0
k=0
for m in str(i):
l=l+int(m)
for o in str(dec_to_binary(i)):
k=k+int(o)
if is_prime(i) and is_prime(l) and is_prime(k):
print(i)