Python Programming/Loops/Solutions

< Back to Problems

1. ''Create a program to count by prime numbers. Ask the user to input a number, then print each prime number up to that number.''

For this program we will make a list of possibilities up to the user's input, then test each one using Fermat's little theorem, verifying that it is prime, then adding the prime to a list of primes. Finally we print the list of primes.

Comment: All primes fulfill Fermat's little theorem, but some returned results are "pseudoprimes" which are not primes (e.g. 341=11*31, in python: (((2**341)-2)%341 == 0)). More information about finding primes & proving primality

2. Instruct the user to pick an arbitrary number from 1 to 100 and proceed to guess it correctly within seven tries. After each guess, the user must tell whether their number is higher than, lower than, or equal to your guess.