Programming assignment 1, due this Friday, January 14, 10pm. This is a relatively light assignment once you get oriented. 1. Create a directory to work in, and download the cs141 repository to it. Detailed instructions are available from the course web page (direct link below): http://www.cs.ucr.edu/~neal/2005/cs141/wiki/index.cgi?Cs141_Home/Repository 2. Familiarize yourself with the programming environment. Learn how to use the editor, the terminal window (shell commands), the g++ compiler, simple makefiles. Ask your TA or email the mailing list if you need help. 3. Once you've gotten your repository set up, move the prog1 directory out of the repository directory and into your working directory. Then go into the prog1 directory (where this README file exists). Inspect the files there: fib.cc --- Computes Fibonacci numbers, slow algorithm. gcd.cc --- Euclid's algorithm, discussed in class. Makefile --- Tells 'make' how to compile the fib and gcd programs. README --- This file. bin/show_call_graph include/* --- Various useful utilities: include/timer.h -- may be useful for timing your code include/call_graph.h -- for generating "call graph files" include/inline_map.h -- some perverse C++, used by xml.h include/xml.h -- simple routines used by call_graph.h 4. Type 'make' to generate 'fib' and 'gcd' executables in the current directory. Then run them. For example, type 'fib 10' to compute the 10th Fibonacci number and to generate a call graph file 'fib_call_graph'. 5. Type 'bin/show_call_graph fib_call_graph' to convert the latter file into viewable format (text, postscript, and html). You can use your browser to view these files. First look at the text file (fib_call_graph_0.txt), then the postscript file, then the html file. Play with the html file in your browser til you get the idea about what it is showing. Note that the html file shows timing information. How big do the numbers have to be before gcd3 is usually faster than gcd1? ################################################################ 6. Now, to the actual assignment: ##### part 1) Create a file gcd_count.cc (by modifying gcd.cc if you like). The makefile is already set up to compile gcd_count.cc if you run 'make gcd_count'. Running 'gcd_count i j' (for two positive integers i and j) should print out an integer k such that k is the number of times the gcd3() subroutine (defined in gcd.cc) would be called as a result of calling gcd3(i,j). For example, 'gcd_count 55 89' should return 9. To turn in: just your gcd_count.cc file. ##### part 2) Create a file fib_count.cc (by modifying fib.cc if you like). The makefile is already set up compile fib_count.cc if you run 'make fib_count'. Running fib_count n m' (for two positive integers n and m) should print out an integer k such that k is the number of times the fib() subroutine (defined in fib.cc) would be called WITH AN ARGUMENT of m OR LESS a result of calling fib(n). For example, I think 'fib_count 8 5' should return 63. To turn in: just your fib_count.cc file. ##### part 3) Extra credit. ## 3a) Suppose a hotel has an infinite number of rooms: room 1, room 2, room 3, ... (one for each positive integer). Initially all doors are unlocked. On day 1, a manager walks past the rooms (in order), and locks each one. On day 2, a manager walks past the rooms again. He unlocks every room whose number is a multiple of 2. On day 3, a manager walks past the rooms again. For every room whose number is a multiple of 3, the manager locks the room if it was unlocked, or unlocks it if it was locked. And so on. In general, on the n'th day, for every room whose number is a multiple of n, the manager changes the room to locked if it was unlocked, or makes it unlocked if it was locked. Note that the status of room n is not changed after day n. The question is, for what values of n is it the case that room n is unlocked from day n on? Write a program hotel.cc and compile it to an executable 'hotel'. 'hotel n' should output 0 if room n is unlocked from the nth day on. ### 3b) Write a high-level but complete explanation of your algorithm (not your program, your algorithm!). Write a high-level but complete explanation of why your algorithm is correct. Or, if you're not sure it is correct, explain why. Be honest. Write a one page explanation of the best upper and lower bounds you can get on the running time of your program, as a function of n. Note: for the most credit, your algorithm should run in time MUCH LESS than proportional to n, and your explanation of your algorithm and why it is correct should be clear and complete. If you turn in an algorithm that runs in time proportional to n you won't get much extra credit. To turn in: Your modified Makefile, your program hotel.cc, and your written work (in text, .pdf, or postscript format only). ######################################################################## Just for fun: 1) Can you figure out a closed-form expression for the value returned by 'fib_count n m' (in terms of n and m)? 2) Check out the 3n+1 problem: http://acm.uva.es/problemset/v1/100.html . Want to be famous? For positive integers n, define f(n) = n/2 if n is even, f(n) = 3n-1 if n is odd. Given an integer n, define c(n) to be the smallest number k such that f^k(n) = = 1, where f^k(n) is f applied k times: f(f(f(...f(n)..))) If there is no such number k, define c(n) = infinity. Write a program to compute c(n) given n. To be famous, prove or disprove that c(n) is finite for every positive integer n. It is not known whether this is true or false.