Sympy Introduction¶
Some of the useful functions available in the sympy package: primefactors(), factorint(), isprime(), factorial().
from sympy import *
primefactors(108)
factorint(108)
isprime(108)
Activity 1: using the date format YYYYMMDD, which dates in the current month are prime numbers?
for d in range(20150801,20150832):
print d, isprime(d)
Activity 2: using the date format YYYYMMDD, check if your birth date is prime. If not, find the first prime date after your birthday.
Number Bases¶
The int() function can be used to convert from any number base 2-36 to base 10.
int('1011',2)
In base 16 (hexadecimal), the six letters A-F have a numerical value (in addition to the ten digits 0-9).
int('1F7',16)
In base 36, all 26 letters of the alphabet have a numerical value (in addition to the ten digits 0-9).
x = int('PRIME',36) # 'PRIME' is an even number; definitely not prime!
print x, isprime(x)
factorint(int('PRIME',36)) # prime factorization of 'PRIME'
x = int('BLAH',36) # 'BLAH' is prime!
print x, isprime(int('BLAH',36))
ProjectEuler.net -- Problem #1¶
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
a = range(3,1000,3) # list of all natural numbers less than 1000 that are multiples of 3
b = range(5,1000,5) # list of all natural numbers less than 1000 that are multiple of 5
c = a + b # combine the two lists. THERE WILL BE DUPLICATES
print c,
d = set(c) # convert to a set to remove duplicates
print d,
sum(d) # the solution to problem 1
Resources¶
- Sympy Documentation from the Jupyter help menu
- The Prime Lexicon: big collection of base 36 prime words
- Project Euler: progamming puzzles