Problem 1.R1: How Many Bits?

Similar documents
Computer Memory. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

The type of all data used in a C++ program must be specified

Topic Notes: Bits and Bytes and Numbers

[301] Bits and Memory. Tyler Caraza-Harter

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

The type of all data used in a C (or C++) program must be specified

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

Topic Notes: Bits and Bytes and Numbers

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia Shane Storks

MULTIPLE OPERAND ADDITION. Multioperand Addition

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

CSC 101: Lab Manual#9 Machine Language and the CPU (largely based on the work of Prof. William Turkett) Lab due date: 5:00pm, day after lab session

Bits, Words, and Integers

CIS 121 Data Structures and Algorithms with Java Spring 2018

(page 0 of the address space) (page 1) (page 2) (page 3) Figure 12.1: A Simple 64-byte Address Space

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Object-Oriented Programming

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

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

Henry Lin, Department of Electrical and Computer Engineering, California State University, Bakersfield Lecture 7 (Digital Logic) July 24 th, 2012

Computer Number Systems Supplement

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 104 -Lecture 2 John Y. Park, adapted by C Grasso

Discrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2

EXAM Computer Science 1 Part 1

3.4. FOR-LOOPS 65. for <v a r i a b l e > in < sequence >:

Ex: If you use a program to record sales, you will want to remember data:

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Computing and Programming

Computing and Programming. Notes for CSC The Beauty and Joy of Computing The University of North Carolina at Greensboro

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

ISA 563 : Fundamentals of Systems Programming

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Worksheet - Storing Data

Basics of Computational Geometry

1007 Imperative Programming Part II

Module 1: Information Representation I -- Number Systems

Organisation. Assessment

Conditionals & Loops /

Chapter 4 Introduction to Control Statements

JAVA OPERATORS GENERAL

Object Oriented Software Design

Recursion Chapter 3.5

Computer Architecture

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Accelerating Information Technology Innovation

RELATIONAL AND LOGICAL OPERATORS

Discussion. Why do we use Base 10?

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

CS Bootcamp Boolean Logic Autumn 2015 A B A B T T T T F F F T F F F F T T T T F T F T T F F F

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Basic data types. Building blocks of computation

COS 126 General Computer Science Fall Exam 1

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

CMSC 341 Hashing. Based on slides from previous iterations of this course

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Unit 1 Lesson 4. Introduction to Control Statements

Search Lesson Outline

Mr G s Java Jive. #11: Formatting Numbers

Arithmetic and Bitwise Operations on Binary Data

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Darrell Bethea May 25, 2011

BITS, BYTES, AND INTEGERS

Bits and Bytes. Here is a sort of glossary of computer buzzwords you will encounter in computer use:

Section 05: Solutions

1 Computer arithmetic with unsigned integers

L05 - Negating Statements

COMP 102: Computers and Computing

What did we talk about last time? Examples switch statements

Datatypes, Variables, and Operations

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

And Parallelism. Parallelism in Prolog. OR Parallelism

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

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

This is the basis for the programming concept called a loop statement

Lecture 1: Introduction

CISC 181 Lab 2 (100 pts) Due: March 7 at midnight (This is a two-week lab)

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Binary Values. CSE 410 Lecture 02

Organizing Data The Power of Structure...

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CP122 CS I. Iteration

GB Programming Challenges

Thomas Jefferson Invitational Open in Informatics 2012

Keccak discussion. Soham Sadhu. January 9, 2012

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Midterm Exam. CS381-Cryptography. October 30, 2014

Types, Expressions, and States

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Transcription:

CSC 495 Problem Set 1 Due Tuesday, January 17 Problem 1.R1: How Many Bits? Required Problem Points: 50 points Background When a number is stored in a primitive type, like an int or long variable, it always uses the same number of bits (32 or 64, typically). But if we don t need to store the leading 0 bits, we might want to know how many bits are actually required to represent a number. For example, the number 13 in binary is 1101, which requires only 4 bits to write down. Note: For a useful example of when you need to know how many bits are required to store a number or a counter, see problem 6 in Column 1 of Programming Pearls (or Problem 1.C2 in this problem set!). The Problem to Solve You are to read a sequence of numbers, and for each one output how many bits are required to store that number. The numbers will all be in the range 1,...,10 18. You are required to compute the answer using only basic boolean operations you may not just call a library function! Hints and Techniques The allowable range for input numbers is important pay attention to it, and make sure you can handle all numbers in that range! Elegance Considerations Avoid hard-coding numbers or making ugly special-case code. Your goal is a very clean and simple solution!

Input and Output The input will consist of one line with the count of how many numbers follow (call this number n), and then n lines each containing a single number in the range 1,...,10 18. For each of the n numbers you should print the number of bits required to represent that number. Sample Input 4 13 5 40000 5555555555555 Sample Output 4 3 16 43

Problem 1.R2: Counting Prime Numbers Required Problem Points: 50 points Ninja Points: 5 extra ninja points to the fastest solution, and any within 5% of the fastest time. Background An important and interesting area of Mathematics studies the distribution and density of prime numbers. In number theory, they use the function π(x) to represent The number of prime numbers less than or equal to x (this is not related in any way to the geometric constant π it just happens to use the same Greek letter). As an example, π(15) = 6 since there are 6 prime numbers less than or equal to 15 (specifically, 2, 3, 5, 7, 11, and 13). There is no known way to compute π(x) efficiently for large x, so people consider techniques for approximating π(x). It turns out that we could approximate π(x) with pretty high accuracy if an unproven Mathematical hypothesis, known as the Riemann Hypothesis, is true. Settling this unsolved Mathematics problem is in fact one of the Clay Institute Millennium Prize Problems, a list from 2000 of what the Clay Institute considered to be the seven most important unsolved Mathematics problems. Anyone who solves this problem will be awarded a million dollars by the Clay Institute. Fortunately, you do not need to solve the Riemann Hypothesis for this problem (however, 1,000,000 ninja points and an automatic T-shirt if you do!). The Problem to Solve In order to consider approximations for π(x), it is important to know what the exact value is for different values of x. You are to write a program that computes π(1, 000, 000, 000) the number of prime numbers less than or equal to a billion. Hints and Techniques Consider using a boolean array with indices from 1 to 1,000,000,000, called isprime[]. We will set the entry isprime[x] to true if and only if x is a prime number. If you can fill in this array, then you can simply count the number of true entries to see how many prime numbers there are. To fill in the array, generalize the following procedure: first, assume everything 2 is prime (so set all entries to true). Then starting at 4 (which is 2*2), step through the boolean array and mark every other entry false, since these are the multiples of 2 (this is sometimes referred to as going through the array with stride 2). Next, starting at 6 (which is 2*3), walk through with stride 3, setting all multiples of 3 to false. Now continue looking from 3 and find the next true entry remember that we set 4 to false, so the next true entry is 5, and you can mark out all multiples of 5. You can repeat this, looking for the next larger prime and crossing off its multiples, until you have marked out all non-prime values in the array. One question to ask, for efficiency: When can you stop? In other words, if an iteration is crossing off all multiples of prime p, how large does p have to get before you stop? To rephrase

this question a little bit, consider an arbitrary non-prime x 1, 000, 000, 000: Can you fill in m in the statement x must have at least one prime factor m? If you can do that, then you can stop your loop once your prime stride becomes larger than m. Elegance Considerations There are several ways to implement this, and as long as your code can correctly compute π(1, 000, 000, 000) in less than 10 minutes on linux.uncg.edu you will get full correctness points. However, to get full elegance points, you will need to code things efficiently so that your program runs in less than two minutes and uses less than 150 megabytes of memory. To do this in a space-efficient manner, you should use techniques similar to those described in Column 1 of Programming Pearls. Input and Output There is no input for this problem, and the required output is the number of prime numbers that are 1, 000, 000, 000. This number is fairly easy to find on the Internet, but you get no correctness credit just for printing that number. It must be computed correctly!

Problem 1.C1: No Carries Allowed Challenge Problem Ninja Points: This challenge problem is worth up to 20 base ninja points Background Adding numbers in binary is similar to the exclusive-or, or XOR, operation, as you can see in the following table (here ^ denotes the XOR operation): x y x^y x+y 0 0 0 0 0 1 1 1 1 0 1 1 1 1 0 10 Note that the least significant bit of the sum is always equal to the XOR of the two input bits, but since the output of the XOR is a single bit it has no place to put the carry bit that you get in the sum. When you use the XOR operator (^) in C, C++, or Java, it operates on all bits in parallel, and is called a bitwise operator since it works on each bit position independently. For some numbers, the XOR of the numbers is the same as the sum, and for others it isn t. To take two examples, 17 and 6 give the same result: 17+6 = 10001 + 110 = 10111 17^6 = 10001 ^ 110 = 10111 but 21 and 6 give different results: 21+6 = 10101 + 110 = 11011 21^6 = 10101 ^ 110 = 10011 The Problem to Solve Given a integers x and y, you can consider the set E = {y x + y = x XOR y}, which is the set of all values that produce the same results when added and XORed with x. We would like to be able to find the kth smallest element in the set E for any input x. x and k will be in the range 1,...,10 18, and you are further guaranteed that the answer will be in the range 0,...,10 18. Your program will be given at most 5 seconds to process each pair of numbers. Hints and Techniques The range of the input numbers is important! It s pretty easy to do this for small numbers, but once numbers get large (say around 10 12 ), if you don t use the correct algorithm then your program will not be be able to produce an answer in a reasonable amount of time. The correct

algorithm will run in time proportional to the number of bits in the input numbers, not the values of the numbers. If you can t figure out the algorithm right away, write out the numbers in the sample in put and output in binary, and look for patterns for a clue. If your program cannot process all numbers in the specified range in under 5 seconds for each pair, then it is not correct. However, some partial credit (but very little at most 5 ninja points!) will be given if your program produces correct answers in under 5 seconds when the input values are limited to the range 1,...,10 6. Input and Output The input will consist of one line with the count of how many pairs follow (call this number n), and then n lines each containing a pair of values x and k. For each pair, you should output the k-th smallest number y for which x + y = x^y. Sample Input 5 1 8 7 1 17 3 21 4 23261 133 Sample Output 14 0 4 10 131328

Problem 1.C2: Most Popular BFF Challenge Problem Ninja Points: This challenge problem is worth up to 20 base ninja points Special Note: The submission program will only run tests on your program on smaller inputs (a million items or less). The full test takes a while to run (just reading the file takes some time, since it is over 3 Gigabytes!). Therefore you will not get any feedback on whether your program is fast enough on the big data set you need to be able to figure out on your own whether your program is fast enough. Submissions will each be tested a single time on the large input, after the due date when submissions are final. Background Sometimes you need to count a lot of items, requiring a lot of space, and so you need to use the most space-efficient representation possible. Imagine if we held a contest to see which person in the United States was best friend to the most other people. Forgetting all the privacy aspects of Social Security Numbers (SSNs), we will ask each person for the SSN of their best friend since SSNs are convenient and unique identifying numbers. How would we process this data to find which SSN was given most often? The Problem to Solve You are given a list of SSNs, each 9 digits long and without dashes (so the range is 100000000 to 999999999), in no particular order. You are guaranteed that no number appears more than 30 times (some people might be popular, but come on...). You are to find which number appears most often, and how often it occurs. Your program should be able to process around 300,000,000 SSN entries in under 15 minutes (but see the special note above). Hints and Techniques See problem 6 at the end of Column 1 in Programming Pearls. Elegance Considerations Note that to have an array with count value for each SSN, where each count is a 4-byte int, would require 4 Gigabytes of memory. While we are seeing more and more systems with 4 GB of RAM, this is too much to reliably count on. You should code this so that it takes no more than 1 GB of memory. There will be up to 310 million numbers provided (the approximate population of the United States).

Input and Output Format The input will consist of one line with the count of how many lines follow (call this number n), and then n lines each containing a single SSN. The output of your program should be a single line of output, with two numbers on it: The most commonly occurring number first, and the count of how many times it occurs second. If there is more than one number with the same most occurring count, output the smallest such number. Note that the sample input given below is obviously much, much shorter than in the real data. In fact, the algorithm you would want to use for just 10 numbers would be very different from that used for 300,000,000 numbers, but the provided output is still correct! Sample Input 10 735917530 753068294 544633009 735917530 544633009 735917530 544633009 735917530 341766943 753068294 Sample Output 735917530 4