1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.

Slides:



Advertisements
Similar presentations
Converting Numbers Between Bases. While the quotient is not zero…  Divide the decimal number by the new base.  Make the remainder the next digit to.
Advertisements

1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
Radix Conversion Given a value X represented in source system with radix  s, represent the same number in a destination system with radix  d Consider.
1 CS1110 Lec Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p Watch.
 Instructor: Rob Nash  Readings: Berger, Chapter 1; Rosen, Chapter 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Unit 11: Logarithms, day 3 3 properties to Expand and Condense Logarithmic Expressions.
Number System Conversions Lecture L2.2 Section 2.3.
Number Systems and Arithmetic
 Binary Binary  Binary Number System Binary Number System  Binary to Decimal Binary to Decimal  Decimal to Binary Decimal to Binary  Octal and Hexadecimal.
Binary Numbers.
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
Data Representation in Computers. Data Representation in Computers/Session 3 / 2 of 33 Number systems  The additive approach – Number earlier consisted.
Simple Data Type Representation and conversion of numbers
Numbering Systems CS208.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
1 Number SystemsLecture 8. 2 BINARY (BASE 2) numbers.
Numbering Systems. CSCE 1062 Outline What is a Numbering System Review of decimal numbering system Binary representation range Hexadecimal numbering system.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Computer Science 111 Fundamentals of Programming I Number Systems.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Number Systems. Why binary numbers? Digital systems process information in binary form. That is using 0s and 1s (LOW and HIGH, 0v and 5v). Digital designer.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Section 2.6 Representation of numbers. Decimal representation (base 10) Given a positive integer X, the decimal representation of X is a string of digits.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Number Base Conversions
Digital Electronics Octal & Hexadecimal Number Systems.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
Octal to Decimal Hexadecimal DecimalOctal Binary.
1 CS100J 26 Feb 2008 More on Recursion My first job was working in an orange juice factory, but I got canned: couldn't concentrate. Then I worked in the.
while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Octal & Hexadecimal Number Systems
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Number System Base b Use only digits of 0, 1, 2,…., b-1 Positional weights X = a n-1 b n-1 + a n-2 b n-2 + …. + a 0 b = 1x x10 1 +
Divisibility Rules. What Are Divisibility Rules? They are a quick way to tell if one number can be completely divided into another without a remainder.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Remarks on Fast Exp (4/2) How do we measure how fast any algorithm is? Definition. The complexity of an algorithm is a measure of the approximate number.
Properties of Logarithms 3 properties to Expand and Condense Logarithmic Expressions; 1 formula to Change the Base Monday, February 8, 2016.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Computer Studies Today Chapter 19 1 Chapter 19. Computer Studies Today Chapter 19 2 »Information stored in a computer is in two states: –ON –OFF.
ECE 2110: Introduction to Digital Systems Number Systems: conversions.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
ECE 2110: Introduction to Digital Systems Number Systems: conversions.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Introduction To Number Systems
Recitation3: CS 3843 Computer Organization 4th week
Digital Design Chapter One Digital Systems and Binary Numbers
Octal to Decimal Decimal Octal Binary Hexadecimal.
Chapter 2: Integers and Mathematical Induction
The Product Property Definition: The log of a product can be expanded into the SUM of the logs of the factors logb mn = logb m + logb n (EXPANDING) EX:
Data Structures in Java with JUnit ©Rick Mercer
CSE 102 Introduction to Computer Engineering
Convert Binary Number to Octal
Introduction to IT By: Muhammed s. anwar.
CSCI206 - Computer Organization & Programming
CS October 2008 The while loop and assertions
Fundamentals of Programming I Number Systems
CS1110 Lec Oct 2010 More on Recursion
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Digital Electronics and Microprocessors
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
Converting Numbers Between Bases
Presentation transcript:

1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes the word "billion", think about it. A billion seconds ago was A billion minutes ago Jesus was alive. A billion hours ago our ancestors were living in the Stone Age. A billion days ago no creature walked the earth on two feet. A billion dollars lasts 8 hours and 20 minutes at the rate our government spends it.

2 On “fixing the invariant” // {s is the sum of 1..h} s= s + (h+1); h= h+1; // {s is the sum of 1..h}

3 On “fixing the invariant” // {s is the sum of h..n} s = h = 5, n = 8 s= s + (h-1); h= h-1; // {s is the sum of h..n} s = h = 4, n = 8

4 Loop pattern to process a range m..n–1 (if m = n, the range is empty) int h= m; // invariant: m..h–1 has // been processed while (h != n) { Process h; h= h+1; } // { m..n–1 has been processed } // invariant: m..h–1 has // been processed for (int h= m; h != n; h != d+1) { Process h; } // { m..n–1 has been processed }

5 Loop pattern to process a range m..n (if m = n+1, the range is empty) int h= m; // invariant: m..h–1 // has been processed while (h != n+1) { Process h; h= h+1; } // { m..n has been processed } // invariant: m..h–1 // has been processed for (int h= m; h <= n; h= h+1) { Process h; } // { m..n has been processed }

6 Decimal OctalBinary = 2** = 2** = 2** = 2** = 2** = 2**5 … = 2**8 2**n in binary is: 1 followed by n zeros. 2**15 is (in decimal). n is called the (base 2) logarithm of 2**n. The log of = 2**15 is 15. To translate an octal number into a binary number, just translate each of digits: octal 745 is binary

7 Logarithmic algorithm to calculate b**c, for c >= 0 /** = b**c, given c ≥ 0 */ public static int exp(int b, int c) { if (c == 0) return 1; } Rest on identities: b**0 = 1 b**c = b * b**(c-1) for even c, b**c = (b*b)**(c/2) 3*3 * 3*3 * 3*3 * 3*3 = 3**8 (3*3)*(3*3)*(3*3)*(3*3) = 9**4 Algorithm processes binary representation of c Suppose c is 14 (1110 in binary) 1. Test if c is even: test if last bit is 0 2. To compute c/2 in binary, just delete the last bit. Algorithm processes each bit of c at most twice. So if c is 2**15 = 32768, algorithm has at most 2*15 = 30 recursive calls! Algorithm is logarithmic in c, since time is proportional to log c if (c%2 = 0) return exp(b*b, c/2); return b * exp(b, c–1);

8 Iterative version of logarithmic algorithm to calculate b**c, for c >= 0 (i.e. b multiplied by itself c times) /** set z to b**c, given c ≥ 0 */ int x= b; int y= c; int z= 1; // invariant: z * x**y = b**c and 0 ≤ y ≤ c while (y != 0) { if (y % 2 == 0) { x= x * x; y= y/2; } else { z= z * x; y= y – 1; } } // { z = b**c } Rest on identities: b**0 = 1 b**c = b * b**(c-1) for even c, b**c = (b*b)**(c/2) 3*3 * 3*3 * 3*3 * 3*3 = 3**8 (3*3)*(3*3)*(3*3)*(3*3) = 9**4 Algorithm is logarithmic in c, since time is proportional to log c

9 Iterative version of logarithmic algorithm to calculate b**c, for c >= 0 (i.e. b multiplied by itself c times) /** set z to b**c, given c ≥ 0 */ int x= b; int y= c; int z= 1; // invariant: z * x**y = b**c and 0 ≤ y ≤ c while (y != 0) { if (y % 2 == 0) { x= x * x; y= y/2; } else { z= z * x; y= y – 1; } } // { z = b**c } Rest on identities: b**0 = 1 b**c = b * b**(c-1) for even c, b**c = (b*b)**(c/2) 3*3 * 3*3 * 3*3 * 3*3 = 3**8 (3*3)*(3*3)*(3*3)*(3*3) = 9**4 Algorithm is logarithmic in c, since time is proportional to log c

10 Calculate quotient and remainder when dividing x by y x/y = q + r/y 21/4= 4 + 3/4 Property: x = q * y + r and 0 ≤ r < y /** Set q to and r to remainder. Note: x >= 0 and y > 0 */ int q= 0; int r= x; // invariant: x = q * y + r and 0 ≤ r while (r >= y) { r= r – y; q= q + 1; } // { x = q * y + r and 0 ≤ r < y }