Aug-11-2023, 08:19 AM
Maths is definitely not my thing, but I tried like this:
Apart from 2, all prime numbers are odd.
Numbers ending in 0 or 5 are divisible by 5.
Numbers whose cross-sum is divisible by 3 are divisible by 3
11 is a strange number with weird properties, but that's another story!
A function to find prime numbers. I never played with prime numbers before, I hope this works!
Apart from 2, all prime numbers are odd.
Numbers ending in 0 or 5 are divisible by 5.
Numbers whose cross-sum is divisible by 3 are divisible by 3
11 is a strange number with weird properties, but that's another story!
A function to find prime numbers. I never played with prime numbers before, I hope this works!
def isPrime(anum): noP = {'0', '2', '4', '5', '6', '8'} P = {1, 2, 3, 5, 7} word = str(anum) if anum in P: #print(anum, 'is a prime number ... ') return True elif word[-1] in noP: #print(anum, 'is not a prime number.') return False sum_num = 0 for w in word: sum_num = sum_num + int(w) if sum_num % 3 == 0: #print('The sum of', anum, 'is divisible by 3 so', anum, 'is not a prime number.') return False others = {7, 9} for n in others: if anum % n == 0: #print(anum, ' is divisible by', n, 'is not a prime number.') return False print('Can not find a factor for', anum, 'so it must be prime ... ') return TrueYou can get prime numbers using this:
for num in range(3, 2004, 2): if isPrime(num): print('*****************') print('The next prime number is', num) print('*****************')For a given number, find the next prime number:
start = int(input('Enter a number ... ')) if not isPrime(start + 1): count = 1 while not isPrime(start+ count): isPrime(start + count) count +=1 else: print('The next prime number is', start + count)Check if a number is prime:
for n in range(1,2004): if 2003 % n == 0: print(n)But I don't know if this uses too much time!