<<

CS 101 Problem Set-2 (for practice, not to be submitted) Problems requiring iterative solutions

August 11th, 2011

Simple Problems

1. Write programs to calculate N!, for a given input value of N, using each of the three iterative control statements ’while’, ’do while’, and ’for’. 2. Given a positive integer N, write a program to find the sum of its digits. The program should also output individual digits as these are extracted. 3. We have to calculate sum of the terms of a 1 + x + x2 + x3 ..., for a given value of x. x is postive but less than 1. Write a program to find the sum by including all those terms, such that the difference between the last two terms included in the sum is less than 1.0E-8. 4. We can calculate the nth odd by the formula 2n - 1. Write a program to find the sum of N odd for a given value of N. Compare the result with n2. 5. Write a program which accepts two numbers m and n from the user and displays the multiplication tables of m upto m × n. 6. Write a program which accepts a number n between 1 and 100 from the user and displays all numbers between 1 and 1000 not divisible by n. 7. Write a program which displays all numbers between 1 and 1000 which are either divisible by 2 or 5 but not both. 8. Write a program which will find all real roots to the following equations in the range [-10000,10000]: (a) 4x2 + 3x − 12 = 0 (b) x3 + x2 = 78 3 1 (c) 0.001x + x = −12 9. A is defined as a greater than 1 which is divisible by only 1 and itself. Write a program which accepts a number from the user and checks if it is a prime number. 10. We define twin primes as a pair of prime numbers which differ by 2. Write a program to display the first 25 twin primes. 11. Write a program which accepts a number n from the user and displays the smallest prime number greater than n. 12. A is defined as a natural number which can be written as a product of 2 prime numbers (not necessarily distinct). Write a program which prints the first 20 . 13. Write a program which accepts a number n from the user and displays the largest semiprime less than n. 14. Greatest Common (gcd) of two or more non-zero integers is defined as the largest positive integer that divides the numbers without a remainder. For example, the gcd of 8 and 12 is 4.Write a program which accepts two integers from the user and finds their gcd.

1 15. Two integers are said to be coprime (or relatively prime) if they have no common positive divisor other than 1 or, equivalently, if their gcd is 1. Write a program which accepts an integer n and displays all coprime pairs (a, b) where a and b are positive integers less than n. 16. Goldbach’s Conjecture: Every even integer greater than 2 can be expressed as the sum of two primes. This fact has been verified for large values. Write a program to verify Goldbach’s conjecture upto 106.

17. Write a program which accepts a number from the user and displays the sum of digits of the number. For instance if the user enters 1789, then the output should be 25 which is 1+7+8+9. 18. Write a program which accepts a number from the user and displays the product of digits of the number. For instance if the user enters 2341, then the output should be 24 which is 2 × 3 × 4 × 1.

19. A is a number that is the same when written forwards or backwards. For example 11, 101, 110011... Write a program which accepts a numbers and determines if it is palindromic or not. 20. Positive Integers a, b, c are pythagorean triplets if a2 + b2 = c2. Write a program which displays all pythagorean triplets (a, b, c) if c < 100.

21. If the sum of cubes of the digits of a natural number is equal to the number itself, then such a number is called an Armstrong number. For instance 153 is an Armstrong number as 153 = 13 + 53 + 33. Write a program which displays the first ten armstrong numbers. 22. A is a positive integer that is equal to the sum of its proper positive , that is, the sum of its positive divisors excluding the number itself. For instance the proper divisors of 6 are 1, 2 and 3. 6=1+2+3, hence 6 is a perfect number. Write a program which accepts a positive integer n and displays the number of perfect numbers less than n.

23. numbers are the numbers defined by the an = an−1 +an−2, with a0 = 0 and a1 = 1. Write a program which accepts an integer n from the user and displays the nth fibonacci number. 24. We define a function Φ over natural numbers as follows: For every natural number n, Φ(n) is defined to be the number of positive integers less than or equal to n that are coprime to n (i.e. having no common positive factors other than 1). For instance let n = 9,then 1,2,4,5,7,8 are coprime to 9. Hence Φ(9) = 6. Write a program which accepts a number n from the user and displays Φ(n).

Challenging Problems

1. We define for every integer, n a river of integers (that is a sequence), hRmi as follows: R1 = n, and Rk = product of the digits of Rk−1, for all positive integers k > 1. Now we define the river to have stopped or terminated if Rt = 0, 1, 2..., or 9 for some positive integer t. For example let us look at the river of number 134, R1 = 134,R2 = 12,R3 = 2. Hence the river terminates at 2. The length of the river is 3. Assuming all rivers terminate for some finite t (which can be proved), write a program that accepts an integer, r from 0 to 9 from the user and determines the number of numbers less than 10000 whose river terminates at r. Also determine the length of the longest river (of a number less than 10000) which terminates at r. 2. We define a function π over natural numbers as follows: We define π(n), for a given positive integer n, as the least positive integer m such that Fm is divisible by n and Fm+1 when divided by n a th th remainder 1 where Fm and Fm+1 are the m and m+1 fibonacci numbers. This is often referred to as the . For instance let n = 4,then the first few terms of fibonacci series (1,1,2,3,5,8,13,21...) leave the following remainder when divided by 4 - 1,1,2,3,1,0,1,1. Hence π(4) = 6. Write a program which finds all solutions to the equation π(n) = π(n2) for all positive integers n less than 100. 3. Write a program which accepts 2 integers m and n and a prime p from the user and checks if there exist m primes such that the sum of these primes is equal to p − n.

2