Starting from:

$30

MATH6015 Math Programming Assignment #2

MATH6015
Math Programming Assignment #2

1. [30 points] Write a function that computes the exact value of n!.
(a) The function must be named factorial verbatim.
(b) The function must take a single non-negative integer as input and return a single integer as output.
Note that 0! = 1.
(c) Do not use Stirling's formulause the exact denition of factorial.
(d) Do NOT use the factorial function from any libraryyou must write your own!
2. [30 points] Write a function that determines if a positive integer is a decimal narcissistic number. A
positive, N-digit integer x written in base-10 (decimal), represented by
x = dN−1 × 10N−1 + dN−2 × 10N−2 + . . . + a2 × 102 + d1 × 101 + d0 × 100
,
is decimal narcissistic if
d
N
N−1 + d
N
N−2 + . . . + d
N
2 + d
N
1 + d
N
0 = x.
For example, all positive, single-digit integers less than ten are decimal narcissistic. Also, 153 is decimal
narcissistic since
153 = 1 × 102 + 5 × 101 + 3 × 100 = 13 + 53 + 33
.
(a) The function must be named is_narcissistic verbatim.
(b) The function must take a single non-negative integer as input and return a boolean True/False as
output.
3. [30 points] Using your is_narcissistic function (do NOT rewrite or copy/paste your code), write a
function that returns the rst N decimal narcissistic numbers starting with 1.
(a) The function must be named find_narcissistic verbatim.
(b) The function must take a single non-negative integer N as input and return a list of the N integers.
4. [30 points] Write a function that estimates the value of π to twelve decimal places using the xed-point
iteration,
xn+1 = xn + sin (xn),
using the initial guess x0 = 3.
(a) The function must be named compute_pi verbatim.
(b) The function will not take any parameters and will return a oating-point number as output.
(c) Do NOT use the denition of π from any libraryyou must write your own!
MATH6015 Page 1/1

More products