def prime(m): if m in [2, 3, 5, 7, 11, 13, 17, 19]: return True if m < 2 or m%2 == 0: return False d = 3 while d*d <= m: if m%d == 0: return False d += 2 return True def gcd(a, b): while b != 0: (a, b) = (b, a%b) if a >= 0: return a else: return (-a) def factor(m): '''Factorize an integer number m. Return a list of pairs (p, e), where p is a prime factor and e is its exponent''' res = [] d = 2 while m > 1 and d*d <= m: if m%d == 0: m //= d e = 1 while m%d == 0: m //= d e += 1 res.append((d, e)) d += 1 if m > 1: res.append((m, 1)) return res def divizors(m): '''Return a list of all proper divizors of integer m''' ''' res = [] d = 1 while d < m: if m%d == 0: res.append(d) d += 1 return res ''' return [d for d in range(1, m) if m%d == 0] def perfect(m): '''Check whether an integer m is a perfect number''' return sum(divizors(m)) == m def semiperfect(m): '''Check whether an integer m is a semiperfect number, i.e. m equals a sum of some subset of its proper divizors''' d = divizors(m) (s, l) = knapsack(d, m) # res = knapsack(d, m) # return res[0] return s def knapsack(l, m): ''' Input: l is a list of positive integers m >= 0 -- integer number Return: a pair (True, s), where a list s is a subset of l such that sum(s) == m, or a pair (False, []), if such subset does not exist. ''' ### is not implemented yet... ### To do... return (False, []) def happy(number): n = len(number) k = n//2 s0 = 0; s1 = 0 for i in range(n): if i < k: s0 += number[i] else: s1 += number[i] return s0 == s1 def nextNumber(number): i = len(number) - 1 c = 1 while i >= 0 and c != 0: if number[i] < 9: number[i] += c c = 0 else: number[i] = 0 i -= 1 return c def happyTickets(n): res = [] ticket = [0]*n while True: if happy(ticket): res.append(ticket.copy()) c = nextNumber(ticket) if c == 1: break return res def numHappyTickets(n): return len(happyTickets(n)) def main(): print("Compute first n prime numbers") s = input("n: ") try: n = int(s) k = 0 m = 2 while k < n: if prime(m): print(m) k += 1 m += 1 except ValueError: print("This is not an integer number.") if __name__ == "__main__": main()