Computer Science 15-112,
Fall 2012
Class Notes: Practice (through week3)
Read these
instructions first!
- These are intended to be solved with loops and conditionals. No
strings, lists, or recursion.
Additional Practice
Here are a bunch of functions you should be able to write now.
These are taken from various recent versions of the 15-112 course notes, so
you can Google to find the full descriptions and usually also some test
functions. Any of these may appear (directly or modified) on a quiz or
exam!
Graphics Problems
These links contain many pictures we can draw, but they also contain some
(perhaps many) we cannot yet draw. Focus on the pictures that can be drawn
with loops and basic shapes (lines, rectangles, ovals, arcs, etc).
-
Flags of the world
- Simple geometric patterns (checkerboards,
tessellations,
other patterns, ...)
Loop and Conditional Problems
No strings, no imports, no recursion
- digitCount(n, digit) # digitCount(123423526, 2) returns
3
- mostFrequentDigit(n)
- hasConsecutiveDigits(n)
- longestDigitRun(n) # longestDigitRun(11777332) returns 3 (because
there is a run of 3 consecutive 7's)
- longestBitRun(n) # same problem, but in base 2, so longestBitRun(0b1011101101)
returns 3
- setBitCount(n) # returns number of bits set to 1 in non-negative number n,
so setBitCount(0b101101001) returns 5).
- goldbachPrime(n) # For even n>0, returns the smallest prime p such that
(n-p) is also prime
- is/nthRotation(x,y) # eg, 3412 is a rotation of 1234, and 321 (with an
implicit leading 0) is a rotation of 3210
- is/nthPalindromicNumber(n) # 12321 is palindromic
- is/nthPerfectNumber(n) # 28 is perfect because its proper divisors are 1,
2, 4, 7, 14 and 1 + 2 + 4 + 7 + 14 = 28
- is/nthPrime(n) # know both the simple and the faster versions of isPrime
- is/nthEmirpsPrime(n) # Primes such that the number resulting from reversing
their digits is also prime
- is/nthAdditivePrime(n) # Primes such that the sum of their digits is also
prime
- is/nthCarolPrime(n) # Primes of the form (2n-1)2 - 2
for some integer n
- is/nthCircularPrime(n) # Primes such that any cyclic rotation of its digits
remains prime
- is/nthDihedralPrime(n) # Primes that remain prime when read upside down or
mirrored n a seven-segment LED display
- is/nthFibonacciPrime(n) # Primes in the Fibonacci sequence
- is/nthIsolatedPrime(n) # Primes p such that neither p-2 nor p+2 are prime
- is/nthLeftTruncatablePrime(n) # Primes that remain prime when the leading
digit is successively removed
- is/nthMarkovPrime(n) # Primes p for which there exist integers x
and y such that x2 + y2 + p2 = 3xyp
- is/nthMersennePrime(n) # Primes of the form 2n-1 for some
integer n
- is/nthPalindromicPrime(n)
- is/nthSelfPrime(n) # Primes that are
self numbers (cannot be
generated by adding any number to the sum of its digits)
- is/nthWeaklyPrime(n) # Primes where any change of any digit always results
in a composite number
- is/nthKaprekarNumber(n) # a Kaprekar number is a non-negative integer, the
representation of whose square can be split into two parts (where the right part
is not zero) that add up to the original number again. For instance, 45 is a
Kaprekar number, because 45**2 = 2025 and 20+25 = 45.
- is/nthWithProperty309(n) # We will say that a number n has "Property309" if
its 5th power contains every digit (from 0 to 9) at least once. 309 is the
smallest number with this property.
- is/nthSquareSumOfTwoSquares(n) # takes a non-negative int n and returns the
nth number that is itself a perfect square and is also the sum of two other
positive perfect squares. For example, the first such number is 25, because
25=5**2, and 25 = 16 + 9 = 4**2 + 3**2.
carpe diem - carpe
diem - carpe diem - carpe diem - carpe diem - carpe diem -
carpe diem - carpe diem - carpe diem