Sie sind auf Seite 1von 4

CSCI 121 Homework 3

Adam Groce
Due: Sunday, September 20 at 11:59 PM

1 is_prime (Partnered problem)


A prime number is a positive integer with exactly two positive integer divisors. One of those
positive divisors is 1 and the other is the number itself. Write a function is_prime that,
when given an integer parameter n, returns True when n is prime and False otherwise.
Here is an example interaction:
>>> is_prime (3)
True
>>> is_prime (6)
False
>>> is_prime (1)
False
>>> is_prime (11)
True
>>> is_prime (9)
False

2 sum_cubes
Write the function sum_cubes, which takes an integer n and returns the sum of the first n
(positive) perfect cubes. For example, sum_cubes(3) should return 36, because 13 +23 +33 =
1 + 8 + 27 = 36. For inputs less than 1, your function should return 0.
>>> sum_cubes (3)
36
>>> sum_cubes ( -1)
0
>>> sum_cubes (2)
9

3 fibonacci3
The Fibonacci sequence begins 1, 1, 2, 3, 5, 8. Each number in the sequence (except for the
first two) is gotten by adding together the two numbers before it. You could define another

1
sequence, which we’ll call the 3-Fibonnacci sequence, by starting with three 1s and then
getting each number by adding the previous three. The function fibonacci3 should take
a positive integer n and compute the nth number in this sequence. The sequence starts
1, 1, 1, 3, 5, 9, 17, 31. For example, the 17 is gotten by adding 3 + 5 + 9.
>>> fibonacci3 (2)
1
>>> fibonacci3 (4)
3
>>> fibonacci3 (9)
57

4 polymax
Consider the function z = −x4 + 3x2 − y 4 + 5y 2 . We are interested in the value of z when
x and y are integers. Your function polymax should take as input minimum and maximum
values for x and y and should return the maximum value z can take when x and y are in
the specified ranges. (Allow x and y to take values equal to the entered ends of the ranges.)
For example, polymax(0,5,3,10) should return the the maximum value that z can take
when x is between 0 and 5 (including 0 and 5 as possibilities) and y is between 3 and 10.
>>> poly2max (0 ,5 ,0 ,10)
6

5 pyramid (Partnered problem)


Let’s draw some art in the terminal. The asterisk pyramid of height 3 is the following
pattern:
*
* *
* * *

It consists of three lines. The first line starts with some spaces and then ends with an
asterisk. The second line has several spaces, an asterisk, a space, and then an asterisk. The
third line starts with an asterisk, then alternates between a space and an asterisk, ending
with an asterisk. Here is the asterisk pyramid of height 4:
*
* *
* * *
* * * *

Like the one of height 3, the last line starts without spaces.
Write a function pyramid that gets a positive integer as input and returns the string
corresponding to the asterisk pyramid of that height. (Note that the function doesn’t
actually print the pyramid.)
Here is an example terminal interaction:

2
>>> pyramid (3)
’ *\ n * *\ n * * *\ n ’
>>> s = pyramid (3)
>>> print ( s )
*
* *
* * *

>>> pyramid (1)


’*\n ’
>>>

Note that the print(s) line above results in a blank line below the pyramid. That is
because the string output by pyramid has a newline symbol at the end, and print is
essentially adding a newline symbol of its own.

6 three_prod
Let’s say that a “three-product” is a way of writing a given number as the product of three
positive integers, a, b, and c, with a ≤ b ≤ c. For example, one three-product for 24 is
1 × 1 × 24. Another is 2 × 2 × 6. The complete set of them is listed below:
1 × 1 × 24
1 × 2 × 12
1×3×8
1×4×6
2×2×6
2×3×4
As you can see, there are 6 three-products for 24. You should write a function three_prod
that takes as input a single positive integer and returns the number of three-products that
exist for that integer. (Remember that they are required to be in non-decreasing order — you
shouldn’t count multiple orderings of the same three numbers as different three-products.)
>>> three_prod (24)
6
>>> three_prod (1)
1
>>> three_prod (17)
1
>>> three_prod (128)
8
>>> three_prod (96)
12

3
7 collatz
Consider the following process. You start with some integer. If the number is odd, multiply
it by 3 and add 1. If it is even, divide it by 2. For every number that has ever been tried, this
process eventually leads to the number 1. (It is an open problem, the Collatz Conjecture,
whether this is true for all integers.) For example, starting with 6 yields the sequence 6,
3, 10, 5, 16, 8, 4, 2, 1. That is a sequence of length 9. Your goal is to write code that
will search for very long Collatz sequences. The function collatz should take as input an
integer n, and return the first integer for which the generated generated sequence is at least
n in length. (Note: This is not the same as the length of the sequence that starts at n. For
example, collatz(11) should return 7, because the Collatz sequence starting at 7 is the
first one that’s at least 11 numbers long.)
>>> collatz (11)
7
>>> collatz (8)
3
>>> collatz (100)
27

8 numpal
A palindrome is a sequence that reads the same forwards as it does backwards. The decimal
digits of some integers form palindromes. For example, 747, 2112, and 8 are such numbers,
as are 77777 and 777777.
Write a function numpal that takes a non-negative integer and returns True if that
number’s digit sequence is a palindrome. It should return False otherwise.
>>> numpal (747)
True
>>> numpal (8)
True
>>> numpal (777777)
True
>>> numpal (123)
False

Note: As in the last homework, do not convert the input to a string in order to solve this
problem. You should be using integer arithmetic.

Das könnte Ihnen auch gefallen