Basic number theory
-
Prove the following:
Theorem (Some properties of divisibility)
Let \(a,b,c \in \mathbb{Z}\) and \(a \neq 0\). Then:
i. if \(a \mid b\) and \(a \mid c\), then \(a \mid (b+c)\);
ii. if \(a \mid b\), then \(a \mid kb\) for any integer \(k\);
iii. if \(a \mid b\) and \(b \mid c\), then \(a \mid c\). - List all the divisors, proper divisors, and prime divisors of 28.
- Let \(n,d\in\mathbb{Z}^+\), how many integers less than \(n\) are divisible by \(d\)?
- If someone claims that given integers \(a,b,c\in \mathbb{Z}^+\), if \(a\mid bc\), then either \(a\mid b\) or \(a\mid c\). What do you say?
- Provide a definition of a composite number using the concept of proper divisors.
-
Here is a definition:
Definition 1 (trivial divisors)
Given an integer \(n\), we say that 1,-1,\(n\), and \(-n\) are the trivial divisors of \(n\).Define prime and composite numbers using the concept of trivial divisors.
- Prove that for any integer \(n\), if \(n\) is even, then \(n^2\) is even. Use the definitions of even numbers and divisibility in your proof.
- Prove that the product of an even integer with an odd integer is even.
- Prove that for any composite integer \(n\), \(n = ab\) for some \(a,b \in \mathbb{Z}\) where \(1 < a < n\) and \(1 < b < n\).
- Prove that if \(a\) and \(b\) are integers with \(a\mid b\), then \(a\) is odd or \(b\) is even.
- Prove that if \(a\) and \(b\) are nonzero integers, \(a\mid b\) and \(a+b\) is odd, then \(a\) is odd.
- Prove that if \(a\) is an integer that is not divisible by 3, then \((a +1)(a +2)\) is divisible by 3.
- Prove that if \(a\) is a positive integer, then 4 does not divide \(a^2 +2\).
-
Define
digit_count(n, base=10)that returns the number of digits ofnin basebase.Criteria and notes
- Everything needs to stay as number, no conversion to string or any other sequence.
- You can use
mathmoduleslog(n, base), but be careful about rounding errors; the workaround is to calllogwith a very small amount added ton, say1e-12; and don’t forget to convert what you get fromlogtoint.
-
Define
bconvert(n, fr=10, to=2)that converts a given number from basefrto baseto.Criteria and notes
- Assume the base will always be less than or equal to 10.
- First generate string or sequence output, this is simpler to find.
- Do not look at solutions on the web until you burn your fuses.
- Once the string/sequence solution is at hand, solve the problem by returning a number that mimics the result. For instance this version must return the decimal number
101to the callbconvert(5,10,2).
-
Define a function
isprime(n).Criteria and notes
- Apply the naive iterative method of checking divisors from 2 upto to
n. - To increase efficiency, consider the following facts:
- If \(n\) is composite, \(n= ab\) with \(1< a < \sqrt{n}\) or \(1< b < \sqrt{n}\)
- All primes greater than 3 can be written in the form \(6k \pm 1\) for some integer \(k\).
Solutions
With
while:def isprime(n): """Return True if n is prime.""" if n <= 1 or n % 2 == 0 or n%3 == 0: return False divides = lambda a,b: b % a == 0 divisor = 5 while divisor * divisor <= n: if divides(divisor,n) or divides(divisor+2,n): return False divisor += 6 return TrueWith
proc:def isprime(n): """Return True if n is prime.""" if n <= 1 or n % 2 == 0 or n%3 == 0: return False divides = lambda a,b: b % a == 0 return proc((True, 5), lambda s: s[1]*s[1] <= n and s[0], lambda s: ((not (divides(s[1],n) or divides(s[1]+2,n)), s[1]+6)))[0] - Apply the naive iterative method of checking divisors from 2 upto to