CS 1101 - ICP (Internet Computer Protocol) Programming Assignment

Download Solution Order New Solution

Assignment Task

Instructions Write python programs for the following.

1. Write a program to approximate

where x, b are inputs given by the user.

2. Write a program that interacts with the user in the following way.

(a) Prompt the user: "Enter a number: ". Accept a number n from the user.

(b) Print the first 5 multiples of that number (example: if the number is 2, then print "2, 4, 6, 8, 10")

(c) Prompt the user: "Continue (y/n)?". Accept an answer.

(d) If the answer is "y", print the next 5 multiples. Then repeat the prompt in step (c).

(e) If the answer is "n", then prompt: "Change numbers (y/n)?"

(f) If the answer is "y", start again from step (a).

(g) If the answer is "n", then exit the program.

3. The guessing game works as follows. The program generates a random number between 1 and 50. The user now has to guess what the number is. The interaction works as follows.

(a) A random number m is generated.

(b) Prompt the user: "Guess the number: ". Accept an input n from the user.

(c) If the number n is less then half of m, then output the message: "The number is much too small." Otherwise, output the message: "The number is too small." Repeat the prompt in step (b).

(d) If the number n is more than one and a half times of m, then output the message: "The number is way too big." Otherwise, output the message: "The number is too big." Repeat the prompt in step (b).

(e) If n is equal to m, then output: "You guessed correctly! Repeat the game? (y/n)". Accept an input from the user.

(f) If the input is "y", then repeat the game starting from step (a). If the input is "n", then exit the game.

Note: Random numbers can be generated using the following piece of code:
import random
random.randint (1,50) # Generates a random integer between 1 and 50

4. Write iterative Python programs (using while loops) for all problems of Assignment 2.

Develop ML functions for the following problems. For all cases provide correctness arguments and estimate the time and space complexities.

5. Computing the nth Fibonacci problem which is inductively defined as fib(0) 1, fib(1) = 1, fib(n) = fib(n - 1) + fib(n-2) for n ≥ 2. Also develop an iterative algorithm for the same problem.

6. The integer square root of n is the integer k such that k2 2. The integer square root can be computed using the following inductive process:

(a) Compute the integer square root i of mn div 4 recursively. We then have that i2 2.

(b) Since m and i are integers, we have that (m + 1) < (i + 1)2. We thus have (21)24m ≤ n < 4m>2.

(c) Hence we have that the integer square root of n is either 2 or 2i+1.
Write a recursive ML program corresponding to the above algorithm. Prove its correctness and derive the number of steps required.

7. Amicable numbers are pairs of numbers each of whose proper divisors add up to the other (1 is included as a divisor but the numbers are not included as their own divisors). The smallest pair of amicable numbers are 220 and 284. Develop a functional algorithm to determine whether a given pair of numbers are amicable or not.

8. We developed a program in the class for testing whether a number is a perfect number or not which required checking for divisors O(n) times. Pease develop a program that may work in O(n) time.

9. We normally express an integer in the base 10 (decimal) representation. For example, 962 = 9 x102+ 6 x101+ 2 x 1000. Similarly, we can express an integer in base 2 (also called binary) representation. For example, 10 1 x 23+0 x 22+ 1 x 21+0 x 20, so its binary representation would be 1010.

Please write a program to covert an input integer from its decimal representation to binary.

This CS 1101 - IT Computer Science has been solved by our PhD Experts at My Uni Paper.

Get It Done! Today

Country
Applicable Time Zone is AEST [Sydney, NSW] (GMT+11)
+

Every Assignment. Every Solution. Instantly. Deadline Ahead? Grab Your Sample Now.