Introduction to Computer Science

Total Page:16

File Type:pdf, Size:1020Kb

Introduction to Computer Science Introduction to Computer Science S. Arun-Kumar [email protected] Department of Computer Science and Engineering I. I. T. Delhi, Hauz Khas, New Delhi 110 016. October 12, 2013 © S. Arun-Kumar HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 1 OF 887 QUIT Contents 1 Computing 3 1.1 Introduction to Computing................................................................................ 3 1.2 Long Multiplication.................................................................................... 26 1.2.1 The algorithm................................................................................... 27 1.2.2 Correctness.................................................................................... 29 1.2.3 The Effort of Long Multiplication........................................................................ 30 1.2.4 Comparison.................................................................................... 31 1.3 Our Computing Tool................................................................................... 35 1.4 Some Simple Algorithmic Definitions.......................................................................... 64 1.5 Substitution of functions................................................................................. 69 1.6 Functions as inductively defined computational processes.............................................................. 79 1.7 Algorithmic Definitions which are well-founded.................................................................... 79 1.8 Recursive processes................................................................................... 84 1.9 Correctness........................................................................................ 86 1.10 Complexity........................................................................................ 88 1.11 Efficiency, Why and How?................................................................................ 89 1.12 Asymptotic analysis and Orders of growth....................................................................... 91 1.13 The Analysis of Algorithms: The Big “O” notation................................................................... 93 2 More examples of recursive algorithms 113 3 introduction to SML 132 3.1 Primitives: Integer & Real................................................................................ 134 HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 2 OF 887 QUIT 3.2 Primitives: Booleans................................................................................... 182 3.3 The Conditional...................................................................................... 185 3.4 The Binary Boolean Operators............................................................................. 200 3.5 Nontermination and Boolean Operators........................................................................ 204 3.6 Example: Fibonacci................................................................................... 214 3.7 Fibonacci Numbers and the Golden Ratio....................................................................... 239 4 Algorithms: Design & Refinement 241 4.1 Technical Completeness & Algorithms......................................................................... 241 4.2 Algorithm Refinement.................................................................................. 276 4.3 Variations: Algorithms & Code............................................................................. 309 4.4 Names, Scopes & Recursion.............................................................................. 340 5 Fermat’s Factorization Method 376 6 Introducing Reals 387 6.1 Floating Point....................................................................................... 387 6.2 Root Finding, Composition and Recursion....................................................................... 412 7 Correctness, Termination & Complexity 446 7.1 Termination and Space Complexity........................................................................... 446 7.2 Efficiency Measures and Speed-ups.......................................................................... 486 7.3 Invariance & Correctness................................................................................ 517 8 Compound Data 543 8.1 Tuples & Lists....................................................................................... 543 HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 3 OF 887 QUIT 8.2 Polynomial Evaluation.................................................................................. 556 8.3 Generating Primes.................................................................................... 569 8.4 Compound Data & Lists................................................................................. 585 8.5 Compound Data & List Algorithms........................................................................... 616 9 Higher Order Functions & Structured Data 646 9.1 Higher Order Functions................................................................................. 646 9.2 Structured Data...................................................................................... 673 9.3 Generalized Sorting Algorithms............................................................................. 686 9.4 User Defined Structured Data Types.......................................................................... 715 10 Imperative Programming: An Introduction 737 10.1 Introducing a Memory Model.............................................................................. 737 10.2 Imperative Programming:................................................................................ 766 10.3 Arrays........................................................................................... 793 11 A large Example: Tautology Checking 807 11.1 Large Example: Tautology Checking.......................................................................... 807 11.2 Tautology Checking Contd................................................................................ 832 12 Lecture-wise Index to Slides 863 HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 4 OF 887 QUIT Colour Coding of text • Black is normal foreground colour. • Brown is the colour of slide headings. • Various colours like brown, red and blue are used to provide emphasis or to stand out from the usual text. • The special colour magenta is used to denote a hyperlink. These hy- perlinks are “mouse-clickable” and they take you to a different slide often out of order. You may use the GO BACK button to return in the reverse order of the traversal of hyperlinks. • Blue type-writer font is used for SML keywords, system re- sponses while running programs etc. HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 5 OF 887 QUIT 1. Computing 1.1. Introduction to Computing 1. Introduction 2. Computing tools 3. Ruler and Compass 4. Computing and Computers 5. Primitives 6. Algorithm 7. Problem: Doubling a Square 8. Solution: Doubling a Square 9. Execution: Step 1 10. Execution: Step 2 11. Doubling a Square: Justified 12. Refinement: Square Construction 13. Refinement 2: Perpendicular at a point 14. Solution: Perpendicular at a point 15. Perpendicular at a point: Justification Next: Our Computing Tool HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 6 OF 887 QUIT Introduction • This course is about computing • Computing as a process is nearly as fundamental as arithmetic • Computing as a mental process • Computing may be done with a variety of tools which may or may not assist the mind HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 7 OF 887 QUIT Computing tools • Sticks and stones (counting) • Paper and pencil (an aid to mental computing) • Abacus (still used in Japan!) • Slide rules (ask a retired engineer!) • Straight-edge and compass HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 8 OF 887 QUIT Straight-edge and Compass Actually it is a computing tool. • Construct a length that is half of a given length • Bisect an angle • Construct a square that is twice the area of a given square p • Construct 10 HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 9 OF 887 QUIT Straight-edge and Compass: Primitives A more exact description of ruler and compass primitives Straight-edge: Can specify lines, rays and line segments. Cannot really specify lengths since the ruler is only supposed to be an unmarked straight-edge. Compass: 1. Can define arcs and circles, 2. Can specify only comparative non-zero lengths and not absolute lengths. Straight-edge and Compass: Can specify points of intersection. Note that an unmarked straight-edge cannot be used to compare lengths. HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 10 OF 887 QUIT Problem: Doubling a Square Consider an example from “Straight-edge and Compass Constructions”. Given a square, construct another square of twice the area of the original square. A D B C Note. • Whatever lengths we assume in the solution have to be taken only as symbolic and not absolute since with an unmarked straight-edge and compass only relative comparisons are possible – no absolute lengths are possible. • The (symbolic) lengths we assume are necessary only in the justifica- tion of correctness of the construction. HOME PAGE CONTENTS JJ J I II GO BACK FULL SCREEN CLOSE 11 OF 887 QUIT Solution & Justification: Doubling a Square Assume given a square ABCD of side a > 0. p 1. Draw the diagonal AC. AC = 2a 2 2. Complete the square ACEF on side AC. Area of ACEF = 2a . Note. • Even though this is a
Recommended publications
  • Unit Name: Powers and Multiples of 10
    Dear Parents, We will begin our next unit of study in math soon. The information below will serve as an overview of the unit as you work to support your child at home. If you have any questions, please feel free to contact me. I appreciate your on-going support. Sincerely, Your Child’s Teacher Unit Name: Powers and Multiples of 10 Common Core State Standards: 5.NBT.1 Recognize that in a multi-digit number, a digit in one place represents 10 times as much as it represents in the place to its right and 1/10 of what it represents in the place to its left. 5.NBT.2 Explain patterns in the number of zeros of the product when multiplying a number by powers of 10, and explain patterns in the placement of the decimal point when a decimal is multiplied or divided by a power of 10. Use whole-number exponents to denote powers of 10. Essential Vocabulary: place value decimal number powers of 10 expanded form Unit Overview: Students extend their understanding of the base-ten system by looking at the relationship among place values. Students are required to reason about the magnitude of numbers. Students should realize that the tens place is ten times as much as the ones place, and the ones place is 1/10th the size of the tens place. New at Grade 5 is the use of whole number exponents to represent powers of 10. Students should understand why multiplying by a power of 10 shifts the digits of a whole number that many places to the left.
    [Show full text]
  • A Family of Sequences Generating Smith Numbers
    1 2 Journal of Integer Sequences, Vol. 16 (2013), 3 Article 13.4.6 47 6 23 11 A Family of Sequences Generating Smith Numbers Amin Witno Department of Basic Sciences Philadelphia University 19392 Jordan [email protected] Abstract Infinitely many Smith numbers can be constructed using sequences which involve repunits. We provide an alternate method of construction by introducing a generaliza- tion of repunits, resulting in new Smith numbers whose decimal digits are composed of zeros and nines. 1 Introduction We work with natural numbers in their decimal representation. Let S(N) denote the “digital sum” (the sum of the base-10 digits) of the number N and let Sp(N) denote the sum of all the digital sums of the prime factors of N, counting multiplicity. For instance, S(2013) = 2+0+1+3 = 6 and, since 2013 = 3 × 11 × 61, we have Sp(2013) = 3+1+1+6+1 = 12. The quantity Sp(N) does not seem to have a name in the literature, so let us just call Sp(N) the p-digit sum of N. The natural number N is called a Smith number when N is composite and S(N)= Sp(N). For example, since 22 = 2 × 11, we have both S(22) = 4 and Sp(22) = 4. Hence, 22 is a Smith number—thus named by Wilansky [4] in 1982. Several different ways of constructing Smith numbers are already known. (See our 2010 article [5], for instance, which contains a brief historical account on the subject as well as a list of references.
    [Show full text]
  • Basic Math Quick Reference Ebook
    This file is distributed FREE OF CHARGE by the publisher Quick Reference Handbooks and the author. Quick Reference eBOOK Click on The math facts listed in this eBook are explained with Contents or Examples, Notes, Tips, & Illustrations Index in the in the left Basic Math Quick Reference Handbook. panel ISBN: 978-0-615-27390-7 to locate a topic. Peter J. Mitas Quick Reference Handbooks Facts from the Basic Math Quick Reference Handbook Contents Click a CHAPTER TITLE to jump to a page in the Contents: Whole Numbers Probability and Statistics Fractions Geometry and Measurement Decimal Numbers Positive and Negative Numbers Universal Number Concepts Algebra Ratios, Proportions, and Percents … then click a LINE IN THE CONTENTS to jump to a topic. Whole Numbers 7 Natural Numbers and Whole Numbers ............................ 7 Digits and Numerals ........................................................ 7 Place Value Notation ....................................................... 7 Rounding a Whole Number ............................................. 8 Operations and Operators ............................................... 8 Adding Whole Numbers................................................... 9 Subtracting Whole Numbers .......................................... 10 Multiplying Whole Numbers ........................................... 11 Dividing Whole Numbers ............................................... 12 Divisibility Rules ............................................................ 13 Multiples of a Whole Number .......................................
    [Show full text]
  • Download This PDF File
    International Journal of Electrical and Computer Engineering (IJECE) Vol. 10, No. 2, April 2020, pp. 1469~1476 ISSN: 2088-8708, DOI: 10.11591/ijece.v10i2.pp1469-1476 1469 The new integer factorization algorithm based on fermat’s factorization algorithm and euler’s theorem Kritsanapong Somsuk Department of Computer and Communication Engineering, Faculty of Technology, Udon Thani Rajabhat University, Udon Thani, 41000, Thailand Article Info ABSTRACT Article history: Although, Integer Factorization is one of the hard problems to break RSA, many factoring techniques are still developed. Fermat’s Factorization Received Apr 7, 2019 Algorithm (FFA) which has very high performance when prime factors are Revised Oct 7, 2019 close to each other is a type of integer factorization algorithms. In fact, there Accepted Oct 20, 2019 are two ways to implement FFA. The first is called FFA-1, it is a process to find the integer from square root computing. Because this operation takes high computation cost, it consumes high computation time to find the result. Keywords: The other method is called FFA-2 which is the different technique to find prime factors. Although the computation loops are quite large, there is no Computation loops square root computing included into the computation. In this paper, Euler’s theorem the new efficient factorization algorithm is introduced. Euler’s theorem is Fermat’s factorization chosen to apply with FFA to find the addition result between two prime Integer factorization factors. The advantage of the proposed method is that almost of square root Prime factors operations are left out from the computation while loops are not increased, they are equal to the first method.
    [Show full text]
  • 1 General Questions 15 • Fibonacci Rule: Each Proceeding [Next] Term Is the Sum of the Previous Two Terms So That
    General 1 Questions 1. WHY DO I HAVE TO LEARN MATHEMATICS? The “why” question is perhaps the one encountered most frequently. It is not a matter of if but when this question comes up. And after high school, it will come up again in different formulations. (Why should I become a mathemat- ics major? Why should the public fund research in mathematics? Why did I ever need to study mathematics?) Thus, it is important to be prepared for this question. Giving a good answer is certainly difficult, as much depends on individual circumstances. First, you should try to find an answer for yourself. What was it that convinced you to study and teach mathematics? Why do you think that mathematics is useful and important? Have you been fascinated by the elegance of mathematical reasoning and the beauty of mathematical results? Tell your students. A heartfelt answer would be most credible. Try to avoid easy answers like “Because there is a test next week” or “Because I say so,” even if you think that the question arises from a general unwill- ingness to learn. It is certainly true that everybody needs to know a certain amount of elementary mathematics to master his or her life. You could point out that there are many everyday situations where mathematics plays a role. Certainly one needs mathematics whenever one deals with money—for example, when one goes shopping, manages a savings account, or makes a monthly budget. Nevertheless, we encounter the “why” question more frequently in situ- ations where the everyday context is less apparent.
    [Show full text]
  • Math 3345-Real Analysis — Lecture 02 9/2/05 1. Are There Really
    Math 3345-Real Analysis — Lecture 02 9/2/05 1. Are there really irrational numbers? We got an indication last time that repeating decimals must be rational. I’ll look at another example, and then we’ll expand that into a quick proof. Consider the number x = 845.7256. The first four digits don’t repeat, but after that the sequence of three digits 256 repeats. If we multiply x by 103 = 1000, we get (1) 1000x = 845,725.6256 That’s part of the trick. Now we subtract (2) 1000x − x = 999x = 845,725.6256 − 845.7256 = 844,879.9000 Therefore, we can write x as 844,879.9 8,448,799 (3) x = = , 999 9990 which is a fraction of integers. The repeating decimal x must be a rational number. Theorem 1. A repeating decimal must be rational. (1) Let x be a repeating decimal. (We want to show that x being rational follows from this assumption.) (2) After some point, we must have n digits repeating forever. (This is what it means to be repeating.) (3) y =10nx − x is a terminating decimal. (When you subtract, the repeating decimals cancel out.) y (4) x = 10n−1 is a fraction of a terminating decimal and an integer. (Basic computation.) (5) x must be a fraction of integers. (You can make any terminating decimal into an integer by multi- plying by a power of 10.) (6) x is rational. (Rationals are fractions of integers.) 1.1. So are all integer fractions repeating decimals? As an example, divide 11 into 235 using long division.
    [Show full text]
  • Modern Computer Arithmetic (Version 0.5. 1)
    Modern Computer Arithmetic Richard P. Brent and Paul Zimmermann Version 0.5.1 arXiv:1004.4710v1 [cs.DS] 27 Apr 2010 Copyright c 2003-2010 Richard P. Brent and Paul Zimmermann This electronic version is distributed under the terms and conditions of the Creative Commons license “Attribution-Noncommercial-No Derivative Works 3.0”. You are free to copy, distribute and transmit this book under the following conditions: Attribution. You must attribute the work in the manner specified • by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial. You may not use this work for commercial purposes. • No Derivative Works. You may not alter, transform, or build upon • this work. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to the web page below. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author’s moral rights. For more information about the license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ Contents Contents iii Preface ix Acknowledgements xi Notation xiii 1 Integer Arithmetic 1 1.1 RepresentationandNotations . 1 1.2 AdditionandSubtraction . .. 2 1.3 Multiplication . 3 1.3.1 Naive Multiplication . 4 1.3.2 Karatsuba’s Algorithm . 5 1.3.3 Toom-Cook Multiplication . 7 1.3.4 UseoftheFastFourierTransform(FFT) . 8 1.3.5 Unbalanced Multiplication . 9 1.3.6 Squaring.......................... 12 1.3.7 Multiplication by a Constant .
    [Show full text]
  • Unit 2: Exponents and Equations
    Georgia Standards of Excellence Curriculum Frameworks Mathematics 8th Grade Unit 2: Exponents and Equations Georgia Department of Education Georgia Standards of Excellence Framework GSE Grade 8 • Exponents and Equations Unit 2 Exponents and Equations TABLE OF CONTENTS OVERVIEW ........................................................................................................................3 STANDARDS ADDRESSED IN THIS UNIT ...................................................................4 STANDARDS FOR MATHEMATICAL PRACTICE .......................................................4 STANDARDS FOR MATHEMATICAL CONTENT ........................................................4 BIG IDEAS ..........................................................................................................................5 ESSENTIAL QUESTIONS .................................................................................................6 FLUENCY ...........................................................................................................................6 SELECTED TERMS AND SYMBOLS ..............................................................................7 EVIDENCE OF LEARNING ..............................................................................................8 FORMATIVE ASSESSMENT LESSONS (FAL) ..............................................................9 SPOTLIGHT TASKS ..........................................................................................................9 3-ACT TASKS..................................................................................................................
    [Show full text]
  • Number Theory Theory and Questions for Topic Based Enrichment Activities/Teaching
    Number Theory Theory and questions for topic based enrichment activities/teaching Contents Prerequisites ................................................................................................................................................................................................................ 1 Questions ..................................................................................................................................................................................................................... 2 Investigative ............................................................................................................................................................................................................. 2 General Questions ................................................................................................................................................................................................... 2 Senior Maths Challenge Questions .......................................................................................................................................................................... 3 British Maths Olympiad Questions .......................................................................................................................................................................... 8 Key Theory ..................................................................................................................................................................................................................
    [Show full text]
  • User's Guide to PARI / GP
    User’s Guide to PARI / GP C. Batut, K. Belabas, D. Bernardi, H. Cohen, M. Olivier Laboratoire A2X, U.M.R. 9936 du C.N.R.S. Universit´eBordeaux I, 351 Cours de la Lib´eration 33405 TALENCE Cedex, FRANCE e-mail: [email protected] Home Page: http://pari.math.u-bordeaux.fr/ Primary ftp site: ftp://pari.math.u-bordeaux.fr/pub/pari/ last updated 11 December 2003 for version 2.2.7 Copyright c 2000–2003 The PARI Group ° Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions, or translations, of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. PARI/GP is Copyright c 2000–2003 The PARI Group ° PARI/GP is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY WHATSOEVER. Table of Contents Chapter1: OverviewofthePARIsystem . ...........5 1.1Introduction ..................................... ....... 5 1.2ThePARItypes..................................... ...... 6 1.3Operationsandfunctions. ...........9 Chapter 2: Specific Use of the GP Calculator . ............ 13 2.1Defaultsandoutputformats . ............14 2.2Simplemetacommands. ........21 2.3InputformatsforthePARItypes . .............25 2.4GPoperators ....................................... .28 2.5ThegeneralGPinputline . ........31 2.6 The GP/PARI programming language . .............32 2.7Errorsanderrorrecovery. .........42 2.8 Interfacing GP with other languages .
    [Show full text]
  • The Power of 10
    DIVISION OF AGRICULTURE RESEARCH & EXTENSION University of Arkansas System Strategy 16 United States Department of Agriculture, University of Arkansas, and County Governments Cooperating Live “The Power of 10” We must not, in trying to think about how much we can make a big difference, ignore the small daily differences which we can make which, over time, add up to big differences that we often cannot forsee.” – Marion Wright Edelman The number “10” is powerful and fits a “small steps approach” to behavior change. It is easy to multiply, divide and remember; small enough not to discourage people from taking action; and large enough to make an impact over time. “10” also shows up repeatedly in expert recommendations to improve health and increase wealth. Whether it’s shedding 10 pounds, exercising in 10-minute increments, saving 10 percent of gross income or reducing debt by $10 a day, “10” and derivatives of 10 (e.g., 1 and 100) are strong motivators if the magnitude of their impact is fully appreciated. Sondra Anice Barnes once made the following comment about taking action to change: “It’s so hard when I have to, and so easy when I want to.” Use “The Power of 10” to increase your “want to factor” (e.g., desire to make a change). The more you know about weight loss and financial planning, the more motivated you will be to make health and wealth changes because you’ll better appreciate the difference that small steps can make. Below are some health-related examples of “The Power of 10”: The Power of 10 – Health Examples Power of 10 Health Improvement Practice Examples and Description Set an initial weight loss goal of 10 percent of As an example, an appropriate initial weight loss goal body weight, to be achieved over a six‐month would be 18 pounds for someone who currently weighs period (i.e., gradual weight loss of 1 to 180 pounds and is overweight or obese.
    [Show full text]
  • Improved Arithmetic in the Ideal Class Group of Imaginary Quadratic Number Fields with an Application to Integer Factoring
    University of Calgary PRISM: University of Calgary's Digital Repository Graduate Studies The Vault: Electronic Theses and Dissertations 2013-06-14 Improved Arithmetic in the Ideal Class Group of Imaginary Quadratic Number Fields with an Application to Integer Factoring Sayles, Maxwell Sayles, M. (2013). Improved Arithmetic in the Ideal Class Group of Imaginary Quadratic Number Fields with an Application to Integer Factoring (Unpublished master's thesis). University of Calgary, Calgary, AB. doi:10.11575/PRISM/26473 http://hdl.handle.net/11023/752 master thesis University of Calgary graduate students retain copyright ownership and moral rights for their thesis. You may use this material in any way that is permitted by the Copyright Act or through licensing that has been assigned to the document. For uses that are not allowable under copyright legislation or licensing, you are required to seek permission. Downloaded from PRISM: https://prism.ucalgary.ca UNIVERSITY OF CALGARY Improved Arithmetic in the Ideal Class Group of Imaginary Quadratic Number Fields With an Application to Integer Factoring by Maxwell Sayles A THESIS SUBMITTED TO THE FACULTY OF GRADUATE STUDIES IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF MASTER OF SCIENCE DEPARTMENT OF COMPUTER SCIENCE CALGARY, ALBERTA May, 2013 c Maxwell Sayles 2013 Abstract This thesis proposes practical improvements to arithmetic and exponentiation in the ideal class group of imaginary quadratic number fields for the Intel x64 architecture. This is useful in tabulating the class number and class group structure, but also as an application to a novel integer factoring algorithm called \SuperSPAR". SuperSPAR builds upon SPAR using an idea from Sutherland, namely a bounded primorial steps search, as well as by using optimized double-based representations of exponents, and general improvements to arithmetic in the ideal class group.
    [Show full text]