if
statement:¶a = 101
if a%2 == 0:
print('even')
else:
print('odd')
for
loop¶for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
print(n**3)
cubes = []
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
cubes.append(n**3)
print(cubes)
Note: for
loops work with strings too:
for letter in 'Buffalo':
print(letter + '!')
n = 10
for d in [2, 3, 4, 5, 6, 7, 8, 9]:
if n%d == 0:
print('{} is not a prime number'.format(n))
range()
function:¶range(n)
produces all integers from 0 to n-1:
for x in range(5):
print(x)
range(m, n)
generates integers from m to n-1:
for x in range(2, 5):
print(x)
range(m, n, k)
increments integers by k at each step:
for x in range(1, 11, 3):
if x == 4:
pass
else:
print(x)
n = 12345
for d in range(2,n):
if n%d == 0:
print('{} is not a prime number'.format(n))
Note: range()
does not produce a list, but an iterable object:
s = range(5)
print(s)
range(n)
can be turned into a list using the list()
:
s = list(range(5))
print(s)
break
and continue
:¶break
can be used to immediately terminate a loop:
for letter in "Mississippi":
print(letter)
if letter == 's':
print('letter "s" found!')
break
continue
can be used to immediately go to the next iteration of the loop:
for letter in 'Mississippi':
if letter == 's':
continue
print(letter)
n = 10175765
for d in range(2,n):
if n%d == 0:
print('{} is not a prime number'.format(n))
break
elif d == n-1:
print('it is a prime')
n = 101
prime = True
for d in range(2,n):
if n%d == 0:
print('{} is not a prime number'.format(n))
prime = False
break
if prime:
print('it is a prime')
def multiply(a, b):
c = a*b
print('{}*{} = {}'.format(a, b, c))
multiply(2,3)
multiply(3, 'hello')
def isprime(n):
for d in range(2,n):
if n%d == 0:
print('{} is not a prime number'.format(n))
break
elif d == n-1:
print('it is a prime')
isprime(2**19-1)
def multiply2(a, b):
c = a*b
return c
x = multiply2(3,2)
print(x)
y = multiply2(12,123)
print(y)
Note: A function will exit immediately after it encounters the return
statement:
def f(a):
print('before return')
return 2*a
print('after return')
x = f(100)
print(x)
def isprime(n):
for d in range(2,n):
if n%d == 0:
return False
break
elif d == n-1:
return True
print(isprime(2))
def isprime(n):
for d in range(2,n):
if n%d == 0:
return False
break
elif d == n-1:
return True
return True
isprime(2)
isprime(1)
def isprime(n):
if n < 2:
return False
for d in range(2,n):
if n%d == 0:
return False
break
elif d == n-1:
return True
return True
for n in range(20):
print(n, isprime(n))
def myprimes(n):
primes = []
for d in range(2, n+1):
if isprime(d):
primes.append(d)
return primes
print(myprimes(20))
print(myprimes(23))
def isprime2(n):
if n < 2:
return False
for d in range(2, round(n**0.5) +1):
if n%d == 0:
return False
break
elif d == n-1:
return True
return True
isprime(2**19 -1)
isprime2(2**19 -1)
from time import time
help(time)
time()
time()
st = time()
isprime(2**19 -1)
et = time()
print(et-st)
st = time()
isprime2(2**19 -1)
et = time()
print(et-st)
st = time()
isprime(2**31 -1)
et = time()
print(et-st)
st = time()
print(isprime2(2**31 -1))
et = time()
print(et-st)
def myprimes2(n):
primes = []
for d in range(2, n+1):
if isprime2(d):
primes.append(d)
return primes
m = 23
n = 14
p = 3
m%p == n%p
Def: A number $p$ is prime-like if $$a^p \equiv a \ (mod \ p) $$ for all $0< a < p$.
Note: Every prime number is prime-like.
Def: A number $p$ is a false prime if it is prime-like but not a prime.
The smallest false prime is 561.
isprime(561)
Part 1. Find at least 20 false primes.
Note Note: you can compute (a**p)%k
more efficiently using the pow(a, p, k)
:
(100**50)%7
pow(100, 50, 7)
st = time()
(10**3000000)%111
print(time() - st)
st = time()
pow(10, 3000000, 111)
print(time() - st)
not
, and
, or
¶"not <exp>
" is True if <exp>
if False and False if <exp>
is True:
isprime(100)
not isprime(100)
"<exp1> and <exp2>
" is True only if both expressions are True:
isprime(2) and isprime(5)
isprime(2) and isprime(10)
isprime(4) and isprime(10)
"<exp1> or <exp2>
" is True it at least one expression is True:
isprime(2) or isprime(5)
isprime(2) or isprime(10)
isprime(4) or isprime(10)
for i in range(100):
if isprime(i) and i%10 == 1:
print(i)
for i in range(100):
if isprime(i) and (i%10 == 1 or i%10 == 9):
print(i)
For each false prime find its primary decompositon and analyze it.
For this write a primary(n)
function that gives the primary decompositon of a number n
.
while
loop¶Syntax:
while <boolean expression>:
<code to be executed>
The loop will repeat as long as the boolean expression is true:
from time import sleep
n = 10
while n > 0:
print(n)
sleep(1)
n -= 1
This can be done with a for
loop too:
for s in range(10,0, -1):
print(s)
sleep(1)
n = 31
while n != 1:
if n%2 == 0:
n = int(n/2)
else:
n = 3*n + 1
print(n)