Conditionals !

Size: px
Start display at page:

Download "Conditionals !"

Transcription

1 Conditionals !

2 Computing GCD GCD Problem: Compute the greatest common divisor of two integers. Input: Two integers a and b. Output: The greatest common divisor of a and b. Exercise: Design an algorithm in pseudocode solving this problem.

3 The World s First Nontrivial Algorithm (~300 BC) Theorem: If a > b, then gcd(a, b) = gcd(a b, b). Example gcd(378, 273) = gcd(105, 273) = gcd(105, 168) = gcd(105, 63) = gcd(42, 63) = gcd(42, 21) = gcd(21, 21) = 21 Euclid! Exercise: Implement Euclid s algorithm in pseudocode.

4 Recall: Recursive Gauss RecursiveGauss(n) if n = 0 return 0 else return n + RecursiveGauss(n-1)

5 Recursive Euclid in Go Comments describing! the function! Function Name! Parameters! // Gcd(a,b) computes the greatest common // divisor of a and b func Gcd(a int, b int) int { Return type! comes after the () and before the {! if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) Return statement: stops! executing the function! and gives its value! Function call! (this one is recursive because it calls the function it s in)!

6 Recursive Factorial Exercise: Write a recursive Go function that takes an integer n and returns n! = (n)(n-1) (2)(1).

7 Recursive Fibonacci Exercise: Write a recursive Go function that takes an integer n and returns the n-th Fibonacci number.

8 The Problem with Recursive Fibonacci

9 Can You Spot the Error? func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)

10 Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c) variable c created variable c destroyed

11 Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { var c int c = b - a return Gcd(a, c) c created c destroyed c resurrected c destroyed

12 Can You Spot the Error? Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)

13 Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed

14 Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed var x, y int = 378, 273 var d int = Gcd(x, y) fmt.println( x is, x) fmt.println( y is, y) Think: What is printed?

15 Additional Conditions Boolean Operator Meaning e1 > e2 e1 is greater than e2! e1 < e2 e1 is less than e2! e1 >= e2 e1 is greater than or equal to e2! e1 <= e2 e1 is less than or equal to e2! e1 == e2 e1 is equal to e2! e1!= e2 e1 is not equal to e2!!e1 true if and only if e1 is false! (e1 and e2 can be complicated expressions) Example conditions: a > 10 * b + c 10 == 10 square(10) < !(x*y < 33) Boolean expressions: evaluate to true or false

16 Boolean Operators: AND and OR Boolean Operator Meaning pipe character Often above \ on your keyboard e1 && e2 e1 e2 true if e1 is true AND e2 is true! true if e1 is true OR e2 is true! Truth Table e 1 e 2 e 1 && e 2 e 1 e 2 true! true! true! true! true! false! false! true! false! true! false! true! false! false! false! false!

17 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

18 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

19 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

20 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

21 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

22 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

23 Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && (a*c 3/z < -5 c + a/2 >= 15) Like in math, we can use parentheses to force a different order.!

24 True/False Quiz a = 10 b = 50 a > 10 && b > 20 b==50 a == 10 && b >= 100 a==10 && b < 100 && a*b > 1000 a>5 && b<20 a==0 && b==0 a>20 b < 51 b-a*b > 0 a>5 b>20 && a==0 b==0 a=10 && b=50 a>5 (b>20 && a==0) b==0 a==10 && b >= 100 b == 50

25 Another If/Else Example // returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { if a % 2 == 0 && b % 2 == 0 { // both a and b are even, so return smaller one if a < b { return a else { return b else if a % 2 == 0 { // a is even but b is not return a else if b % 2 == 0 { // only b is even return b else { // both a and b are odd return 0 Why is this line OK? What would happen if we swapped this with the first case? % is the modulus operator: x % y is the remainder when integer x is divided by integer y.

26 Switch Statement switch statements let you express mutually exclusive tests compactly. // Returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { switch { case a % 2 == 0 && b % 2 == 0: if a < b { return a else { return b case a % 2 == 0: return a case b % 2 == 0: return b default: return 0 Each case part contains a condition, followed by a : and then a sequence of statements. The statements associated with the first true case will be executed. The optional default case is executed if none of the others are.

27 wikipedia user EMDX! Switch Is a Railroad Analogy

28 Switch Statement with Doomsday // KeyDay takes the month as input (between 1 and 12) // It returns the key doomsday of the month func KeyDay(month int) int { switch month { case 1: return 31 case 2: return 28 case 3: return 0 case 4: return 4 case 5: return 9 case 6: return 6 case 7: return 11 case 8: return 8 case 9: return 5 case 10: return 10 case 11: return 7 case 12: return 12

29 Switch Statement with the Skew Array 2 skew C ATGGGCATCGGCCATACGCC // SkewValue computes the contribution of an individual // nucleotide to the computation of skew func SkewValue(symbol string) int { switch symbol { case A : return 0 case C : return -1 case G : return 1 case T : return 0

30 Guidelines for Programming 1. Get running programs first, then focus on formatting. 2. Comment your code more often than you think you should. 3. Don t write a single line of code before you plan (think about writing a short story). English first, then pseudocode, then Go. 4. Code the pieces that you know how to code first. 5. Code a little bit every day rather than a lot the night before an assignment is due. 6. Don t get bogged down on a single piece for more than minutes. 7. Compile your code frequently. 8. Negative space is important you should be able to read code easily. 9. One way of doing something well > ten ways of doing it poorly.

Conditionals & Loops /

Conditionals & Loops / Conditionals & Loops 02-201 / 02-601 Conditionals If Statement if statements let you execute statements conditionally. true "then" part condition a > b false "else" part func max(a int, b int) int { var

More information

Functions & Variables /

Functions & Variables / Functions & Variables 02-201 / 02-601 Functions Functions in calculus give a rule for mapping input values to an output: f : R R May take multiple inputs: g : R R R 11 10 9 8 7 6 5-1.0-0.5 0.5 1.0 x f(x)

More information

COP 4516: Math for Programming Contest Notes

COP 4516: Math for Programming Contest Notes COP 4516: Math for Programming Contest Notes Euclid's Algorithm Euclid's Algorithm is the efficient way to determine the greatest common divisor between two integers. Given two positive integers a and

More information

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm MA/CSSE 473 Day 06 Euclid's Algorithm MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm 1 Quick look at review topics in textbook REVIEW

More information

1 Elementary number theory

1 Elementary number theory Math 215 - Introduction to Advanced Mathematics Spring 2019 1 Elementary number theory We assume the existence of the natural numbers and the integers N = {1, 2, 3,...} Z = {..., 3, 2, 1, 0, 1, 2, 3,...},

More information

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion Copyright 2003 Scott/Jones Publishing Topics 19.1 Introduction to Recursion 19.2 The Recursive Factorial Function 19.3 The Recursive

More information

UCT Algorithm Circle: Number Theory

UCT Algorithm Circle: Number Theory UCT Algorithm Circle: 7 April 2011 Outline Primes and Prime Factorisation 1 Primes and Prime Factorisation 2 3 4 Some revision (hopefully) What is a prime number? An integer greater than 1 whose only factors

More information

Names and Functions. Chapter 2

Names and Functions. Chapter 2 Chapter 2 Names and Functions So far we have built only tiny toy programs. To build bigger ones, we need to be able to name things so as to refer to them later. We also need to write expressions whose

More information

Math Introduction to Advanced Mathematics

Math Introduction to Advanced Mathematics Math 215 - Introduction to Advanced Mathematics Number Theory Fall 2017 The following introductory guide to number theory is borrowed from Drew Shulman and is used in a couple of other Math 215 classes.

More information

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15 COMPSCI 230 Discrete Math January 24, 2017 COMPSCI 230 Discrete Math Prime Numbers January 24, 2017 1 / 15 Outline 1 Prime Numbers The Sieve of Eratosthenes Python Implementations GCD and Co-Primes COMPSCI

More information

Solutions to the Second Midterm Exam

Solutions to the Second Midterm Exam CS/Math 240: Intro to Discrete Math 3/27/2011 Instructor: Dieter van Melkebeek Solutions to the Second Midterm Exam Problem 1 This question deals with the following implementation of binary search. Function

More information

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF Copyright Cengage Learning. All rights reserved. SECTION 4.8 Application: Algorithms Copyright Cengage Learning. All rights reserved. Application:

More information

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math Exponentiation x y Can easily compute with a simple algorithm: Runtime? ans = 1 i = y while i > 0: ans = ans * x i-- 2 Just like with multiplication, let s

More information

Add Binary Numbers What is the largest decimal number you can represent using 3 bits?

Add Binary Numbers What is the largest decimal number you can represent using 3 bits? 1 2 Quiz1 Question Add Binary Numbers 1 0 1 1 a) 1 0 1 0 1 0 +1 0 0 0 b) 0 1 0 0 1 1 1 0 0 1 1 c) 0 1 0 0 0 1 d) 0 1 0 1 1 1 e) none 001011 001000 010011 Quiz1 question What is the largest decimal number

More information

Reading 8 : Recursion

Reading 8 : Recursion CS/Math 40: Introduction to Discrete Mathematics Fall 015 Instructors: Beck Hasti, Gautam Prakriya Reading 8 : Recursion 8.1 Recursion Recursion in computer science and mathematics refers to the idea of

More information

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015 Mathematics Jaehyun Park CS 97SI Stanford University June 29, 2015 Outline Algebra Number Theory Combinatorics Geometry Algebra 2 Sum of Powers n k=1 k 3 k 2 = 1 n(n + 1)(2n + 1) 6 = ( k ) 2 = ( 1 2 n(n

More information

Lecture Notes, CSE 232, Fall 2014 Semester

Lecture Notes, CSE 232, Fall 2014 Semester Lecture Notes, CSE 232, Fall 2014 Semester Dr. Brett Olsen Week 11 - Number Theory Number theory is the study of the integers. The most basic concept in number theory is divisibility. We say that b divides

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Chapter 4. Number Theory. 4.1 Factors and multiples

Chapter 4. Number Theory. 4.1 Factors and multiples Chapter 4 Number Theory We ve now covered most of the basic techniques for writing proofs. So we re going to start applying them to specific topics in mathematics, starting with number theory. Number theory

More information

Intro to Computational Programming in C Engineering For Kids!

Intro to Computational Programming in C Engineering For Kids! CONTROL STRUCTURES CONDITIONAL EXPRESSIONS Take the construction like this: Simple example: if (conditional expression) statement(s) we do if the condition is true statement(s) we do if the condition is

More information

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Programming & Data Structure Laboratory. Day 2, July 24, 2014 Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body

More information

More on Strings & Arrays

More on Strings & Arrays More on Strings & Arrays 02-201 Indexing Strings Strings work like arrays in some ways: Strings have fixed length. You can find the length of string s with len(s). You can access elements of string s with

More information

Chapter 1 Programming: A General Overview

Chapter 1 Programming: A General Overview Chapter 1 Programming: A General Overview 2 Introduction This class is an introduction to the design, implementation, and analysis of algorithms. Examples: sorting large amounts of data organizing information

More information

CS302: Self Check Quiz 2

CS302: Self Check Quiz 2 CS302: Self Check Quiz 2 name: Part I True or False For these questions, is the statement true or false? Assume the statements are about the Java programming language. 1.) The result of an expression with

More information

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Introduce Java programming language Assignment and type casting Expressions Operator precedence Code

More information

Lecture Notes on Contracts

Lecture Notes on Contracts Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,

More information

Integers and Mathematical Induction

Integers and Mathematical Induction IT Program, NTUT, Fall 07 Integers and Mathematical Induction Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology TAIWAN 1 Learning Objectives Learn about

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Assertions & Verification & Example Loop Invariants Example Exam Questions

Assertions & Verification & Example Loop Invariants Example Exam Questions 2014 November 27 1. Assertions & Verification & Example Loop Invariants Example Exam Questions 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer

More information

Lecture 10: Recursion vs Iteration

Lecture 10: Recursion vs Iteration cs2010: algorithms and data structures Lecture 10: Recursion vs Iteration Vasileios Koutavas School of Computer Science and Statistics Trinity College Dublin how methods execute Call stack: is a stack

More information

CS 3410 Ch 7 Recursion

CS 3410 Ch 7 Recursion CS 3410 Ch 7 Recursion Sections Pages 7.1-7.4, 7.7 93-319, 333-336 7.1 Introduction 1. A recursive method is a method that either directly or indirectly makes a call to itself. [Weiss]. It does this by

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Recursion June 27, 2017 Tong Wang UMass Boston CS 310 June 27, 2017 1 / 20 Recursion Recursion means defining something, such as a function, in terms of itself

More information

Assertions & Verification Example Exam Questions

Assertions & Verification Example Exam Questions 2009 November 23 Assertions & Verification Example Exam Questions 1. 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

More information

Problem Solving & C M. Tech. (CS) 1st year, 2014

Problem Solving & C M. Tech. (CS) 1st year, 2014 Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24, 2014 Outline 1 C as an imperative language 2 sizeof Operator in C 3 Use

More information

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1 Chapter 1 An Introduction to Computer Science INVITATION TO Computer Science 1 Q8. Under what conditions would the well-known quadratic formula not be effectively computable? (Assume that you are working

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730 MATH 2730 Benjamin V.C. Collins James A. Swenson s and undefined terms The importance of definition s matter! may be more important in Discrete Math than in any math course that you have had previously.

More information

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion 2. Recursion Algorithm Two Approaches to Algorithms (1) Iteration It exploits while-loop, for-loop, repeat-until etc. Classical, conventional, and general approach (2) Recursion Self-function call It exploits

More information

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms Chapter Summary Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms Section 5.1 Sec.on Summary Mathematical Induction Examples of Proof by Mathematical Induction Mistaken

More information

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with

More information

Lecture 7 Number Theory Euiseong Seo

Lecture 7 Number Theory Euiseong Seo Lecture 7 Number Theory Euiseong Seo (euiseong@skku.edu) 1 Number Theory God created the integers. All else is the work of man Leopold Kronecker Study of the property of the integers Specifically, integer

More information

CSc 372 Comparative Programming Languages

CSc 372 Comparative Programming Languages CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

More information

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona 1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists

More information

BEng (Hons) Telecommunications. Examinations for / Semester 2

BEng (Hons) Telecommunications. Examinations for / Semester 2 BEng (Hons) Telecommunications Cohort: BTEL/14B/FT Examinations for 2014-2015 / Semester 2 MODULE: NUMBERS, LOGICS AND GRAPHS THEORIES MODULE CODE: Duration: 3 Hours Instructions to Candidates: 1 Answer

More information

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return

More information

Case by Case. Chapter 3

Case by Case. Chapter 3 Chapter 3 Case by Case In the previous chapter, we used the conditional expression if... then... else to define functions whose results depend on their arguments. For some of them we had to nest the conditional

More information

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value 1 Number System Introduction In this chapter, we will study about the number system and number line. We will also learn about the four fundamental operations on whole numbers and their properties. Natural

More information

Lecture 5: Variables and Functions

Lecture 5: Variables and Functions Carl Kingsford, 0-0, Fall 05 Lecture 5: Variables and Functions Structure of a Go program The Go program is stored in a plain text file A Go program consists of a sequence of statements, usually per line

More information

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna - Iteration Chapter 7 Prof. Mauro Gaspari: gaspari@cs.unibo.it Multiple assigments bruce = 5 print bruce, bruce = 7 print bruce Assigment and equality With multiple assignment it is especially important

More information

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD CSC148, Lab #4 This document contains the instructions for lab number 4 in CSC148H. To earn your lab mark, you must actively participate in the lab. We mark you in order to ensure a serious attempt at

More information

Math 171 Proficiency Packet on Integers

Math 171 Proficiency Packet on Integers Math 171 Proficiency Packet on Integers Section 1: Integers For many of man's purposes the set of whole numbers W = { 0, 1, 2, } is inadequate. It became necessary to invent negative numbers and extend

More information

Genome Sciences 373 Genome Informa1cs. Quiz Sec1on #1 March 31, 2015

Genome Sciences 373 Genome Informa1cs. Quiz Sec1on #1 March 31, 2015 Genome Sciences 373 Genome Informa1cs Quiz Sec1on #1 March 31, 2015 About me, course logistics, etc. Matthew s contact info Email: mwsnyder@uw.edu Phone: 206-685-3720 Office hours: Mondays 2:00-3:00pm

More information

UNIT-II NUMBER THEORY

UNIT-II NUMBER THEORY UNIT-II NUMBER THEORY An integer n is even if, and only if, n equals twice some integer. i.e. if n is an integer, then n is even an integer k such that n =2k An integer n is odd if, and only if, n equals

More information

Exponentiation. Evaluation of Polynomial. A Little Smarter. Horner s Rule. Chapter 5 Recursive Algorithms 2/19/2016 A 2 M = (A M ) 2 A M+N = A M A N

Exponentiation. Evaluation of Polynomial. A Little Smarter. Horner s Rule. Chapter 5 Recursive Algorithms 2/19/2016 A 2 M = (A M ) 2 A M+N = A M A N Exponentiation Chapter 5 Recursive Algorithms A 2 M = (A M ) 2 A M+N = A M A N quickpower(a, N) { if (N == 1) return A; if (N is even) B = quickpower(a, N/2); retrun B*B; return A*quickPower(A, N-1); slowpower(a,

More information

bool bool - either true or false

bool bool - either true or false Strings & Branching bool bool - either true or false You have the common math comparisons: > (greater than), e.g. 7 > 2.5 is true == (equals), e.g. 5 == 4 is false

More information

Issue with Implementing PrimeSieve() in Go

Issue with Implementing PrimeSieve() in Go Slices 02-201 Issue with Implementing PrimeSieve() in Go func PrimeSieve(n int) [n+1]bool { var iscomposite [n+1]bool //ERROR! biggestprime := 2 for biggestprime < n for i:=2; i

More information

CSC 222: Object-Oriented Programming. Spring 2012

CSC 222: Object-Oriented Programming. Spring 2012 CSC 222: Object-Oriented Programming Spring 2012 recursion & sorting recursive algorithms base case, recursive case silly examples: fibonacci, GCD real examples: merge sort, quick sort recursion vs. iteration

More information

1 Elementary number theory

1 Elementary number theory 1 Elementary number theory We assume the existence of the natural numbers and the integers N = {1, 2, 3,...} Z = {..., 3, 2, 1, 0, 1, 2, 3,...}, along with their most basic arithmetical and ordering properties.

More information

Combinational Circuits Digital Logic (Materials taken primarily from:

Combinational Circuits Digital Logic (Materials taken primarily from: Combinational Circuits Digital Logic (Materials taken primarily from: http://www.facstaff.bucknell.edu/mastascu/elessonshtml/eeindex.html http://www.cs.princeton.edu/~cos126 ) Digital Systems What is a

More information

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops Lab 4 while loops and for loops The purpose of this lab is to introduce you to the concept of a for loop, gain experience distinguishing between a while loop (which is a more general type of loop than

More information

Recursion. Chapter 5

Recursion. Chapter 5 Recursion Chapter 5 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how to write recursive algorithms and methods for searching arrays To learn

More information

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd Is the statement sufficient? If both x and y are odd, is xy odd? Is x < 0? 1) xy 2 < 0 Positives & Negatives Answer: Yes, xy is odd Odd numbers can be represented as 2m + 1 or 2n + 1, where m and n are

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

1 / 43. Today. Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm.

1 / 43. Today. Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm. 1 / 43 Today Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm. 2 / 43 Finding an inverse? We showed how to efficiently tell if there is an inverse. Extend euclid to

More information

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS Jaehyun Park Today s Lecture Algebra Number Theory Combinatorics (non-computational) Geometry Emphasis on how to compute Sum of Powers n k=1 k 2 = 1 6 n(n

More information

Creating a new data type

Creating a new data type Appendix B Creating a new data type Object-oriented programming languages allow programmers to create new data types that behave much like built-in data types. We will explore this capability by building

More information

MAT 685: C++ for Mathematicians

MAT 685: C++ for Mathematicians The Extended Euclidean Algorithm John Perry University of Southern Mississippi Spring 2017 Outline 1 2 3 Outline 1 2 3 Bézout s Identity Theorem (Bézout s Identity) Let a,b. We can find x,y such that gcd(a,b)

More information

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Excerpt from Art of Problem Solving Volume 1: the Basics 2014 AoPS Inc. Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Odd-Numbered Answers to Exercise Set 1.1: Numbers

Odd-Numbered Answers to Exercise Set 1.1: Numbers Odd-Numbered Answers to Exercise Set.: Numbers. (a) Composite;,,, Prime Neither (d) Neither (e) Composite;,,,,,. (a) 0. 0. 0. (d) 0. (e) 0. (f) 0. (g) 0. (h) 0. (i) 0.9 = (j). (since = ) 9 9 (k). (since

More information

Recursive Functions. Biostatistics 615 Lecture 5

Recursive Functions. Biostatistics 615 Lecture 5 Recursive Functions Biostatistics 615 Lecture 5 Notes on Problem Set 1 Results were very positive! (But homework was time-consuming!) Familiar with Union Find algorithms Language of choice 50% tried C

More information

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title Flow of Control Branching 2 Cheng, Wei COMP110-001 May 19, 2014 Title Review of Previous Lecture If else Q1: Write a small program that Reads an integer from user Prints Even if the integer is even Otherwise,

More information

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions User Defined Functions In previous labs, you've encountered useful functions, such as sqrt() and pow(), that were created by other

More information

CSCE 110: Programming I

CSCE 110: Programming I CSCE 110: Programming I Sample Questions for Exam #1 February 17, 2013 Below are sample questions to help you prepare for Exam #1. Make sure you can solve all of these problems by hand. For most of the

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

Chapter 3. Set Theory. 3.1 What is a Set?

Chapter 3. Set Theory. 3.1 What is a Set? Chapter 3 Set Theory 3.1 What is a Set? A set is a well-defined collection of objects called elements or members of the set. Here, well-defined means accurately and unambiguously stated or described. Any

More information

ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library

ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library COMP-202B, Winter 2010, All Sections Due: Wednesday, March 3, 2010 (23:55) You MUST do this assignment individually and, unless otherwise

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Today. Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm.

Today. Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm. Today Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm. Finding an inverse? We showed how to efficiently tell if there is an inverse. Extend euclid to find inverse. Euclid s GCD algorithm.

More information

Programming Logic & Pseudocode. Python Bootcamp, Day 1 Anna Rosen

Programming Logic & Pseudocode. Python Bootcamp, Day 1 Anna Rosen Programming Logic & Pseudocode Python Bootcamp, Day 1 Anna Rosen Programming 101 Computer programming involves having the user formulate commands that the computer can run for a specific purpose. The computer

More information

CS 221 Lecture. Tuesday, 11 October 2011

CS 221 Lecture. Tuesday, 11 October 2011 CS 221 Lecture Tuesday, 11 October 2011 "Computers in the future may weigh no more than 1.5 tons." - Popular Mechanics, forecasting the relentless march of science, 1949. Today s Topics 1. Announcements

More information

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14 Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to

More information

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures Outline 1 2 Solution 2: calls 3 Implementation To create recursion, you must create recursion. How to a recursive algorithm

More information

Recursion CS GMU

Recursion CS GMU Recursion CS 112 @ GMU Recursion 2 Recursion recursion: something defined in terms of itself. function recursion: when a function calls itself. Sometimes this happens directly, sometimes indirectly. direct:

More information

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly) Branching Announcements Homework 0: using cin with 10/3 is NOT the same as 3.3333 (directly) With cin, it will stop as soon as it reaches a type that does not match the variable (into which it is storing)

More information

Structured programming

Structured programming Exercises 8 Version 1.0, 1 December, 2016 Table of Contents 1. Recursion................................................................... 1 1.1. Problem 1...............................................................

More information

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar 2 Lecture 2 CS118 Term planner For your first seminar Meet at CS reception Bring The Guide Bring your CS account details Finish the problem sheet in your own time Talk to each other about the questions

More information

Introduction to Scientific Computing

Introduction to Scientific Computing Introduction to Scientific Computing Dr Hanno Rein Last updated: October 12, 2018 1 Computers A computer is a machine which can perform a set of calculations. The purpose of this course is to give you

More information

Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday)

Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday) Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday) Name, class, URL (www.cs.berkeley.edu/~demmel/ma55) on board Head TA Mike West speaks on bureaucracy Advertise CS 70 (T Th 2-3:30) as an "honors"

More information

4. Functions. March 10, 2010

4. Functions. March 10, 2010 March 10, 2010 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 40 Outline Recapitulation Functions Part 1 What is a Procedure? Call-by-value and Call-by-reference Functions

More information

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include b) What using statement do you always put at the top of

More information

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question Page 1 of 6 Template no.: A Course Name: Computer Programming1 Course ID: Exam Duration: 2 Hours Exam Time: Exam Date: Final Exam 1'st Semester Student no. in the list: Exam pages: Student's Name: Student

More information

CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00

CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00 CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00 [18] 1. Consider the following predicate logic statement: 9x 2 A, 8y 2 B,9z 2 C, P(x, y, z)! Q(x, y, z), where A, B,

More information

Data Dependences and Parallelization

Data Dependences and Parallelization Data Dependences and Parallelization 1 Agenda Introduction Single Loop Nested Loops Data Dependence Analysis 2 Motivation DOALL loops: loops whose iterations can execute in parallel for i = 11, 20 a[i]

More information

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element.

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element. The first exam will be on Wednesday, September 22, 2010. The syllabus will be sections 1.1 and 1.2 in Lax, and the number theory handout found on the class web site, plus the handout on the method of successive

More information

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt ECE 2574: Data Structures and Algorithms - Recursion Part I C. L. Wyatt Today we will introduce the notion of recursion, look at some examples, and see how to implement them in code. Introduction to recursion

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

WYSE Academic Challenge Computer Science Test (State) 2012 Solution Set

WYSE Academic Challenge Computer Science Test (State) 2012 Solution Set WYSE Academic Challenge Computer Science Test (State) 2012 Solution Set 1. Correct Answer: B. The aspect ratio is the fractional relation of the width of the display area compared to its height. 2. Correct

More information