hw 2 - 1536 is even so it is 6 4824 is even so it is 1 It...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1. O(n) Polynomial 2. 1, 2, 3, 5, 7, 10, 13, 15, 19 -------- 1 2 3 4 5 6 7 8, 9 given input x sum = 1 while (x > 1) sum *= x x-- return sum log(N!) = log(1) + log(2) + … log(N) upper bound = O(n 2 ) log(N!) <= log(N N ) = N log N = 2 n n. log N = n. size of number x = log x N/2 log (N/2) = O(N log N) 3. Given input x int square = 1; for (int i = 1; i < x; i++) { square = i*i; if (square > x) return false; else if (square == x) return true; } O(log x) since it’ll exit if the square is > the number 4a. 1 because it has even number of exponents. 4b. Using repeated squaring 2, 4, 8, 16, 32, 64, 128 (2 64 * 2 32 * 2 16 * 2 8 * 2 4 * 2 1 ) % (2 7 – 1) 2 5^3 4c. either ends in 4 or 6, must end in 9 or 1. Only way to be divisible by 35 is 4 – 9, or 6 – 1.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1536 is even, so it is 6. 4824 is even, so it is 1. It is divisible by 5 for sure. Because 35 = 5*7, that means that 4*6 must have remainder 1, and 4824 and 1536 are divisible by 24. Therefore, 1-1 (mod 35) = 0. Yes. 4d. 4, 21, none, 14 5. gcd(Fn+1, Fn) = 1 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … Because F n is the sum of F n-1 + F n-2 , and if we are comparing F n-1 and F n , that means Base case: the gcd(1, 1) = 1, gcd(1, 2) = 1. 6. Input x, y. X*y/GCD(x,y) = LCM. Finding GCD = O(n 3 ) (this algorithm works) 2 7 – 1 1, 1, 2, 3, 5, 8, 13 Using euclid’s formula...
View Full Document

This note was uploaded on 01/09/2012 for the course CSE 101 taught by Professor Staff during the Spring '08 term at UCSD.

Ask a homework question - tutors are online