Calculations. 4.1 Check if a number is a prime

Size: px
Start display at page:

Download "Calculations. 4.1 Check if a number is a prime"

Transcription

1 4 Calculations In this chapter, we ll look at various one-liners for performing calculations, such as finding minimum and maximum elements, counting, shuffling and permuting words, and calculating dates and numbers. You ll also learn about the -a, -M, and -F command-line arguments, the $, special variable, and ]} construction that lets you run code inside double quotes. 4.1 Check if a number is a prime perl -lne (1x$_)!~ /^1?$ ^(11+?)\1+$/ && print "$_ is prime" This one-liner uses an ingenious regular expression by Abigail to detect whether a given number is a prime. (Don t take this regular

2 expression too seriously; I ve included it for its artistic value. For serious purposes, use the Math::Primality module from CPAN to see whether a number is prime.) Here s how this ingenious one-liner works: First, the number is converted into its unary representation by (1x$_). For example, 5 is converted into 1x5, which is (1 repeated 5 times). Next, the unary number is tested against the regular expression. If it doesn t match, the number is a prime; otherwise it s a composite. The!~ operator is the opposite of the =~ operator and is true if the regular expression doesn t match. The regular expression consists of two parts: The first part, ^1?$, matches 1 and the empty string. The empty string and 1 are clearly not prime numbers, so this part of the regular expression discards them. The second part, ^(11+?)\1+$, determines whether two or more 1s repeatedly make up the whole number. If so, the regular expression matches, which means the number is a composite. If not, it s a prime. Now consider how the second part of the regular expression would act on the number 5. The number 5 in unary is 11111, so the (11+?) matches the first two 1s, the back-reference \1 becomes 11, and the whole regular expression now becomes ^11(11)+$. Because it can t match five 1s, it fails. Next, it attempts to match the first three 1s. The back-reference becomes 111, and the whole regular expression becomes ^111(111)+$, which doesn t match. The process repeats for 1111 and 11111, which also don t match, and as a result the entire regular expression doesn t match and the number is a prime. What about the number 4? The number 4 is 1111 in unary. The (11+?) matches the first two 1s. The back-reference \1 becomes 11, and the regular expression becomes ^11(11)+$, which matches the original string and confirms that the number is not prime. 4.2 Print the sum of all fields on each line perl -MList::Util=sum -alne print This one-liner turns on field auto-splitting with the -a command-line option and imports the sum function from the List::Util module with -Mlist::Util=sum. (List::Util is part of the Perl core, so you don t need install it.) Auto-splitting happens on whitespace characters by default, and the resulting fields are put in variable. For example, the line would be split on each space so would become (1, 4, 8). The statement sums the elements in array, giving you Chapter 4

3 The -Mmodule=arg option imports arg from module. It s the same as writing use module qw(arg); This one-liner is equivalent to use List::Util qw(sum); while (<>) = split( ); print "\n"; } You can change auto-splitting s default behavior by specifying an argument to the -F command-line switch. Say you have the following line: 1:2:3:4:5:6:7:8:9:10 And you wish to find the sum of all these numbers. You can simply specify : as an argument to the -F switch, like this: perl -MList::Util=sum -F: -alne print This splits the line on the colon character and sums all the numbers. The output is 55 because that s the sum of the numbers 1 through Print the sum of all fields on all lines perl -MList::Util=sum -alne END { print } This one-liner keeps pushing the split fields to array. Once the input stops and Perl is about to quit, the END { } block is executed and it outputs the sum of all items This sums all fields over all lines. Notice how pushing array to array actually appends elements to it. This differs from many other languages, where pushing array1 to array2 would put array1 into array2, rather than appending the elements of array1 onto array2. Perl performs list flattening by design. Unfortunately, summing all fields on all lines using this solution creates a array. A better solution is to keep only the running sum, like this: perl -MList::Util=sum -alne $s += END { print $s } Calculations 31

4 Here, each line is split and the values are summed and stored in the running sum variable $s. Once all input has been processed, the one-liner prints the value of $s. 4.4 Shuffle all fields on each line perl -MList::Util=shuffle -alne The trickiest part of this one-liner construction. This construction allows you to execute the code inside the quotation marks. Normally text and variables are placed inside quotation marks, but with ]} construction you can run code, too. In this one-liner, the code to execute inside of the quotation marks is which shuffles the fields and returns the shuffled list. The creates an array reference containing the shuffled fields, and } dereferences it. You simply create a reference and immediately dereference it. This allows you to run the code inside the quotation marks. Let s look at several examples to understand why I chose to run the code inside the quotation marks. If I had written print the fields on the line would be concatenated. Compare the output of this one-liner: $ echo a b c d perl -MList::Util=shuffle -alne print b c d a to this: $ echo a b c d perl -MList::Util=shuffle -alne print bcda In the first example, the array of shuffled fields (inside the double quotation marks) is interpolated, and the array s elements are separated by a space, so the output is b c d a. In the second example, interpolation doesn t happen, and Perl simply dumps out element by element without separating them, and the output is bcda. You can use the $, special variable to change the separator between array elements when they re printed. For example, here s what happens when I change the separator to a colon: $ echo a b c d perl -MList::Util=shuffle -alne $,=":"; print b:c:d:a 32 Chapter 4

5 You can also use the join function to join the elements with a space: perl -MList::Util=shuffle -alne print join " ", Here, the join function joins the elements of an array using the given separator, but ]} construction is the cleanest way to do it. 4.5 Find the numerically smallest element (minimum element) on each line perl -MList::Util=min -alne print This one-liner is somewhat similar to the previous ones. It uses the min function from List::Util. Once the line has been automatically split by -a and the elements are in array, the min function finds the numerically smallest element, which it prints. For example, if you have a file that contains these lines: Running this one-liner produces the following output: The smallest number on the first line is -8; on the second line, the smallest number is 0; and on the third line, Find the numerically smallest element (minimum element) over all lines perl -MList::Util=min = END { print } This one-liner combines one-liners 4.3 and 4.5. = construct is the same It appends the contents to array. Calculations 33

6 This one-liner stores all data in memory, and if you run it on a really huge file, Perl will run out of memory. Your best bet is to find the smallest element on every line and compare that element with the smallest element on the previous line. If the element on the current line is less than the previous one, it s the smallest element so far. Once all lines have been processed, you can just print the smallest element found through the END block: perl -MList::Util=min -alne $min = $rmin = $min unless defined $rmin && $min > $rmin; END { print $rmin } Here, you first find the minimum element on the current line and store it in $min. Then you check to see if the smallest element on the current line is the smallest element so far. If so, assign it to $rmin. Once you ve looped over the whole input, the END block executes and you print the $rmin. Say your file contains these lines: Running this one-liner outputs -25 because that s the smallest number in the file. If you re using Perl 5.10 or later, you can do the same thing with this one-liner: perl -MList::Util=min -alne $min = min($min // END { print $min } This one-liner uses the // operator, which is new to Perl This operator is similar to the logical OR operator ( ), except that it tests the left side s definedness rather than the truth. What that means is it tests whether the left side is defined rather than whether it is true or false. In this one-liner, the expression $min // () returns $min if $min has been defined, or else it returns an empty list (). The // operator saves you from having to use defined to test definedness. Consider what happens when this one-liner is run on the previous file. First, Perl reads the line , splits it, and puts the numbers in array. array is now (-8, 9, 10, 5). Next, it executes $min = min ($min // Because $min hasn t been defined, $min // () evaluates to (), so the whole expression becomes $min = min ((), (-8, 9, 10, 5)). 34 Chapter 4

7 Perl does list flattening by design, so after flattening the arguments to the min function, the expression becomes $min = min(-8, 9, 10, 5). This defines $min, setting it to -8. Perl proceeds to the next line, where it to (7, 0, 9, 3) and again evaluates $min = min($min // Because $min has now been defined, $min // () evaluates to $min and the expression becomes $min = min(-8, 7, 0, 9, 3). At this point, -8 is still the smallest element, so $min remains -8. Finally, Perl reads in the last line, and after evaluating $min = min(-8, 5, -25, 9, 999), it finds that -25 is the smallest element in the file. 4.7 Find the numerically largest element (maximum element) on each line perl -MList::Util=max -alne print This works the same as one-liner 4.5, except that you replace min with max. 4.8 Find the numerically largest element (maximum element) over all lines perl -MList::Util=max = END { print } This one-liner is similar to one-liners 4.6 and 4.7. In this one-liner, each line is auto-split and put in array, and then this array is merged with array. When the input has been processed, the END block executes and the maximum element is printed. Here s another way to find the maximum element, keeping just the running maximum element instead of all elements in memory: perl -MList::Util=max -alne $max = $rmax = $max unless defined $rmax && $max < $rmax; END { print $rmax } If you re using Perl 5.10 or later, you can use the // operator to shorten this one-liner: perl -MList::Util=max -alne $max = max($max // END { print $max } This is the same as one-liner 4.6, except you replace min with max. Calculations 35

8 4.9 Replace each field with its absolute value perl -alne print { abs This one-liner first auto-splits the line using the -a option. The split fields end up in variable. Next, it calls the absolute value function abs on each field with the help of the map function. Essentially, the map function applies a given function to each element of the list and returns a new list that contains the results of applying the function. For example, if the is (-4, 2, 0), mapping abs over it produces the list (4, 2, 0). Finally, this one-liner prints the new list of positive values. ]} construct, introduced in one-liner 4.4, allows you to execute the code inside the quotation marks Print the total number of fields on each line perl -alne print This one-liner forces the evaluation in the scalar context, which in Perl means the number of elements As a result, it prints the number of elements on each line. For example, if your file contains the following lines: foo bar baz foo bar baz Running this one-liner produces the following output: The first line has three fields, the second line has two fields, and the last line has one field. 36 Chapter 4

9 4.11 Print the total number of fields on each line, followed by the line perl -alne print " $_" This one-liner is the same as one-liner 4.10, with the addition of $_ at the end, which prints the whole line. (Remember that -n puts each line in the $_ variable.) Let s run this one-liner on the same example file that I used in oneliner 4.10: foo bar baz foo bar baz Running the one-liner produces the following output: 3 foo bar baz 2 foo bar 1 baz 4.12 Print the total number of fields on all lines perl -alne $t END { print $t } Here, the one-liner keeps adding the number of fields on each line to variable $t until all lines have been processed. Next, it prints the result, which contains the number of words on all lines. Notice that you add array to the scalar variable $t. Because $t is scalar, array is evaluated in the scalar context and returns the number of elements it contains. Running this one-liner on the following file: foo bar baz foo bar baz produces the number 6 as output because the file contains a total of six words. Calculations 37

10 4.13 Print the total number of fields that match a pattern perl -alne map { /regex/ && $t++ END { print $t 0 } This one-liner uses map to apply an operation to each element in array. In this example, the operation checks to see if each element matches /regex/, and if it does, it increments the $t variable. It then prints the $t variable, which contains the number of fields that match the /regex/ pattern. The $t 0 construct is necessary because if no fields match, $t wouldn t exist, so you must provide a default value. Instead of 0, you can provide any other default value, even a string! Looping would be a better approach: perl -alne $t += /regex/ END { print $t } Here, each element is tested against /regex/. If it matches, /regex/ returns true; otherwise it returns false. When used numerically, true converts to 1 and false converts to 0, so $t += /regex/ adds either 1 or 0 to the $t variable. As a result, the number of matches is counted in $t. You do not need a default value when printing the result in the END block because the += operator is run regardless of whether the field matches. You will always get a value, and sometimes that value will be 0. Another way to do this is to use grep in the scalar context: perl -alne $t += grep END { print $t } Here, grep returns the number of matches because it s evaluated in the scalar context. In the list context, grep returns all matching elements, but in the scalar context, it returns the number of matching elements. This number is accumulated in $t and printed in the END block. In this case, you don t need to provide a default value for $t because grep returns 0 in those situations Print the total number of lines that match a pattern perl -lne /regex/ && $t++; END { print $t 0 } Here, /regex/ evaluates to true if the current line of input matches this regular expression. Writing /regex/ && $t++ is the same as writing if ($_ =~ /regex/) { $t++ }, which increments the $t variable if the line matches the specified pattern. In the END block, the $t variable contains the total number of pattern matches and is printed; but if no lines match, $t is once again undefined, so you must print a default value. 38 Chapter 4

11 4.15 Print the number π perl -Mbignum=bpi -le print bpi(21) The bignum package exports the bpi function that calculates the π constant to the desired accuracy. This one-liner prints π to 20 decimal places. (Notice that you need to specify n+1 to print it to an accuracy of n.) The bignum library also exports the constant π, precomputed to 39 decimal places: perl -Mbignum=PI -le print PI 4.16 Print the number e perl -Mbignum=bexp -le print bexp(1,21) The bignum library exports the bexp function, which takes two arguments: the power to raise e to, and the desired accuracy. This one-liner prints the constant e to 20 decimal places. For example, you could print the value of e 2 to 30 decimal places: perl -Mbignum=bexp -le print bexp(2,31) As with π, bignum also exports the constant e precomputed to 39 decimal places: perl -Mbignum=e -le print e 4.17 Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC) perl -le print time The built-in time function returns seconds since the epoch. This oneliner simply prints the time. Calculations 39

12 4.18 Print Greenwich Mean Time and local computer time perl -le print scalar gmtime The gmtime function is a built-in Perl function. When used in the scalar context, it returns the time localized to Greenwich Mean Time (GMT). The built-in localtime function acts like gmtime, except it returns the computer s local time when it s used in the scalar context: perl -le print scalar localtime In the list context, both gmtime and localtime return a nine-element list (known as struct tm to UNIX programmers) with the following elements: ($second, [0] $minute, [1] $hour, [2] $month_day, [3] $month, [4] $year, [5] $week_day, [6] $year_day, [7] $is_daylight_saving [8] ) You can slice this list (that is, extract elements from it) or print individual elements if you need just some part of the information it contains. For example, to print H:M:S, slice the elements 2, 1, and 0 from localtime, like this: perl -le print join ":", (localtime)[2,1,0] To slice elements individually, specify a list of elements to extract, for instance [2,1,0]. Or slice them as a range: perl -le print join ":", (localtime)[2..6] This one-liner prints the hour, date, month, year, and day of the week. You can also use negative indexes to select elements from the opposite end of a list: perl -le print join ":", (localtime)[-2, -3] This one-liner prints elements 7 and 6, which are the day of the year (for example, the 200th day) and of the week (for example, the 4th day), respectively. 40 Chapter 4

13 4.19 Print yesterday s date perl -MPOSIX = localtime; $now[3] -= 1; print scalar localtime Remember that localtime returns a nine-item list (see one-liner 4.18) of various date elements. The fourth element in the list is the current month s day. If you subtract 1 from this element, you get yesterday. The mktime function constructs the UNIX epoch time from this modified nine-element list, and the scalar localtime construct prints the new date, which is yesterday. This one-liner also works in edge cases, such as when the current day is the first day of the month. You need the POSIX package because it exports the mktime function. For example, if it s Mon May 20 05:49:55 right now, running this oneliner prints Sun May 19 05:49: Print the date 14 months, 9 days, and 7 seconds ago perl -MPOSIX = localtime; $now[0] -= 7; $now[3] -= 9; $now[4] -= 14; print scalar localtime This one-liner modifies the first, fourth, and fifth elements of list. The first element is seconds, the fourth is days, and the fifth is months. The mktime command generates the UNIX time from this new structure, and localtime, which is evaluated in the scalar context, prints the date 14 months, 9 days, and 7 seconds ago Calculate the factorial perl -MMath::BigInt -le print Math::BigInt->new(5)->bfac() This one-liner uses the bfac() function from the Math::BigInt module in the Perl core. (In other words, you don t need to install it.) The Math::BigInt->new(5) construction creates a new Math::BigInt object with Calculations 41

14 a value of 5, after which the bfac() method is called on the newly created object to calculate the factorial of 5. Change 5 to any number you wish to find its factorial. Another way to calculate a factorial is to multiply the numbers from 1 to n together: perl -le $f = 1; $f *= $_ for 1..5; print $f Here, I set $f to 1 and then loop from 1 to 5 and multiply $f by each value. The result is 120 (1*2*3*4*5), the factorial of Calculate the greatest common divisor perl -MMath::BigInt=bgcd -le print bgcd(@list_of_numbers) Math::BigInt has several other useful math functions including bgcd, which calculates the greatest common divisor (gcd) of a list of numbers. For example, to find the greatest common divisor of (20, 60, 30), execute the one-liner like this: perl -MMath::BigInt=bgcd -le print bgcd(20,60,30) To calculate the gcd from a file or user s input, use the -a commandline argument and pass array to the bgcd function: perl -MMath::BigInt=bgcd -anle print bgcd(@f) (I explained the -a argument and array in one-liner 4.2 on page 30.) You could also use Euclid s algorithm to find the gcd of $n and $m. This one-liner does just that and stores the result in $m: perl -le $n = 20; $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $m Euclid s algorithm is one of the oldest algorithms for finding the gcd. 42 Chapter 4

15 4.23 Calculate the least common multiple The least common multiple (lcm) function, blcm, is included in Math::BigInt. Use this one-liner to find the least common multiple of (35, 20, 8): perl -MMath::BigInt=blcm -le print blcm(35,20,8) To find the lcm from a file with numbers, use the -a command-line switch and array: perl -MMath::BigInt=blcm -anle print blcm(@f) If you know a bit of number theory, you may recall that there is a connection between the gcd and the lcm. Given two numbers $n and $m, you know that their lcm is $n*$m/gcd($n,$m). This one-liner, therefore, follows: perl -le $a = $n = 20; $b = $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $a*$b/$m 4.24 Generate 10 random numbers between 5 and 15 (excluding 15) perl -le print join ",", map { int(rand(15-5))+5 } This one-liner prints 10 random numbers between 5 and 15. It may look complicated, but it s actually simple. int(rand(15-5)) is just int(rand(10)), which returns a random integer from 0 to 9. Adding 5 to it makes it return a random integer from 5 to 14. The range makes it draw 10 random integers. You can also write this one-liner more verbosely: perl -le $n=10; $min=5; $max=15; $, = " "; print map { int(rand($max-$min))+$min } 1..$n; Calculations 43

16 Here, all variables are more explicit. To modify this one-liner, change the variables $n, $min, and $max. The $n variable represents how many random numbers to generate, and $min-$max is the range of numbers for use in that generation. The $, variable is set to a space because it s the output field separator for print and it s undef by default. If you didn t set $, to a space, the numbers would be printed concatenated. (See one-liner 4.4 on page 32 for a discussion of $,.) 4.25 Generate all permutations of a list perl -MAlgorithm::Permute -le $l = [1,2,3,4,5]; $p = Algorithm::Permute->new($l); print "@r" = $p->next This one-liner uses the object-oriented interface of the module Algorithm::Permute to find all permutations of a list, that is, all ways to rearrange items. The constructor of Algorithm::Permute takes an array reference of elements to permute. In this particular one-liner, the elements are the numbers 1, 2, 3, 4, 5. The next method returns the next permutation. Calling it repeatedly iterates over all permutations, and each permutation is put in array and then printed. (Beware: The output list gets large really quickly. There are n! (n factorial) permutations for a list of n elements.) Another way to print all permutations is with the permute subroutine: perl -MAlgorithm::Permute = (1,2,3,4,5); Algorithm::Permute::permute { print "@l" Here s what you get if you to just three elements (1, 2, 3) and run it: Chapter 4

17 4.26 Generate the powerset perl -MList::PowerSet=powerset = (1,2,3,4,5); print "@$_" This one-liner uses the List::PowerSet module from CPAN. The module exports the powerset function, which takes a list of elements and returns a reference to an array containing references to subset arrays. You can install this module by running cpan List::PowerSet at the command line. In the for loop, you call the powerset function and pass it the list of elements Next, you dereference the return value of powerset, which is a reference to an array of subsets, and then dereference each individual and print it. The powerset is the set of all subsets. For a set of n elements, there are exactly 2 n subsets in the powerset. Here s an example of the powerset of (1, 2, 3): Convert an IP address to an unsigned integer perl -le $i=3; $u += ($_<<8*$i--) for " " =~ /(\d+)/g; print $u This one-liner converts the IP address into an unsigned integer by first doing a global match of (\d+) on the IP address. Performing a for loop over a global match iterates over all the matches, which are the four parts of the IP address: 127, 0, 0, 1. Calculations 45

18 Next, the matches are summed in the $u variable. The first bit is shifted 8 3 = 24 places, the second is shifted 8 2 = 16 places, and the third is shifted 8 places. The last is simply added to $u. The resulting integer happens to be (a very geeky number). Here are some more one-liners: perl -le $ip=" "; $ip =~ s/(\d+)\.?/sprintf("%02x", $1)/ge; print hex($ip) This one-liner utilizes the fact that can be easily converted to hex. Here, the $ip is matched against (\d+), and each IP part is transformed into a hex number with sprintf("%02x", $1) inside the s operator. The /e flag of the s operator makes it evaluate the substitution part as a Perl expression. As a result, is transformed into 7f and then interpreted as a hexadecimal number by Perl s hex operator, which converts it to a decimal number. You can also use unpack: perl -le print unpack("n", ) This one-liner is probably as short as possible. It uses vstring literals (version strings) to express the IP address. A vstring forms a string literal composed of characters with the specified ordinal values. The newly formed string literal is unpacked into a number from a string in network byte order (big-endian order) and then printed. If you have a string with an IP (rather than a vstring), you first have to convert it to byte form with the function inet_aton: perl -MSocket -le print unpack("n", inet_aton(" ")) Here, inet_aton converts the string to the byte form (equivalent to the pure vstring ) and then unpack unpacks it, as in the previous one-liner. 46 Chapter 4

19 4.28 Convert an unsigned integer to an IP address perl -MSocket -le print inet_ntoa(pack("n", )) Here, the integer is packed into a number in big-endian byte order and then passed to the inet_ntoa function that converts a number back to an IP address. (Notice that inet_ntoa is the opposite of inet_aton.) You can do the same thing like this: perl -le $ip = ; print join ".", map { (($ip>>8*($_))&0xff) } reverse 0..3 Here, the $ip is shifted 24 bits to the right and then bitwise ANDed with 0xFF to produce the first part of the IP, which is 127. Next, it s shifted 16 bits and bitwise ANDed with 0xFF, producing 0, and then shifted 8 bits and bitwise ANDed with 0xFF, producing another 0. Finally, the whole number is bitwise ANDed with 0xFF, producing 1. The result from map {... } is a list (127, 0, 0, 1). That list is now joined by a dot "." to produce the IP address You can replace join with the special variable $,, which acts as a value separator for the print statement: perl -le $ip = ; $, = "."; print map { (($ip>>8*($_))&0xff) } reverse 0..3 Because reverse 0..3 is the same as 3,2,1,0, you could also write: perl -le $ip = ; $, = "."; print map { (($ip>>8*($_))&0xff) } 3,2,1,0 Calculations 47

20

Perl One-Liners by Peteris Krumins

Perl One-Liners by Peteris Krumins 1 Introduction to Perl one-liners are small and awesome Perl programs that fit in a single line of code. They do one thing really well like changing line spacing, numbering lines, performing calculations,

More information

For instance, if B = 10000, the number is represented internally as [3456, 7890, 12].

For instance, if B = 10000, the number is represented internally as [3456, 7890, 12]. NAME SYNOPSIS DESCRIPTION Math::BigInt::Calc - Pure Perl module to support Math::BigInt This library provides support for big integer calculations. It is not intended to be used by other modules. Other

More information

Pathologically Eclectic Rubbish Lister

Pathologically Eclectic Rubbish Lister Pathologically Eclectic Rubbish Lister 1 Perl Design Philosophy Author: Reuben Francis Cornel perl is an acronym for Practical Extraction and Report Language. But I guess the title is a rough translation

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

1. Introduction. 2. Scalar Data

1. Introduction. 2. Scalar Data 1. Introduction What Does Perl Stand For? Why Did Larry Create Perl? Why Didn t Larry Just Use Some Other Language? Is Perl Easy or Hard? How Did Perl Get to Be So Popular? What s Happening with Perl Now?

More information

Course Learning Outcomes for Unit I. Reading Assignment. Unit Lesson. UNIT I STUDY GUIDE Number Theory and the Real Number System

Course Learning Outcomes for Unit I. Reading Assignment. Unit Lesson. UNIT I STUDY GUIDE Number Theory and the Real Number System UNIT I STUDY GUIDE Number Theory and the Real Number System Course Learning Outcomes for Unit I Upon completion of this unit, students should be able to: 2. Relate number theory, integer computation, and

More information

Advance Praise for Perl One-Liners

Advance Praise for Perl One-Liners Advance Praise for Perl One-Liners One of the slogans used by Perl is Easy things should be easy and hard things should be possible. This book illustrates just how easy things can be and how much can be

More information

CpSc 421 Final Solutions

CpSc 421 Final Solutions CpSc 421 Final Solutions Do any eight of the ten problems below. If you attempt more than eight problems, please indicate which ones to grade (otherwise we will make a random choice). This allows you to

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

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems SCHOOL OF ENGINEERING & BUILT ENVIRONMENT Mathematics Numbers & Number Systems Introduction Numbers and Their Properties Multiples and Factors The Division Algorithm Prime and Composite Numbers Prime Factors

More information

9 abcd = dcba b + 90c = c + 10b b = 10c.

9 abcd = dcba b + 90c = c + 10b b = 10c. In this session, we ll learn how to solve problems related to place value. This is one of the fundamental concepts in arithmetic, something every elementary and middle school mathematics teacher should

More information

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22 COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source

More information

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list. Arrays Perl arrays store lists of scalar values, which may be of different types. They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list. A list literal

More information

pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap

pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap NAME SYNOPSIS List::Util - A selection of general-utility list subroutines use List::Util qw( reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst

More information

Section A Arithmetic ( 5) Exercise A

Section A Arithmetic ( 5) Exercise A Section A Arithmetic In the non-calculator section of the examination there might be times when you need to work with quite awkward numbers quickly and accurately. In particular you must be very familiar

More information

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15 Integer Representation Representation of integers: unsigned and signed Sign extension Arithmetic and shifting Casting But first, encode deck of cards. cards in suits How do we encode suits, face cards?

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data Arithmetic and Bitwise Operations on Binary Data CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides

More information

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS). 2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS). 2.. Natural Binary Code (NBC). The positional code with base 2 (B=2), introduced in Exercise, is used to encode

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

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

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

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2 Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2 Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements Website is up

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS

DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS C H A P T E R 6 DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS OUTLINE 6- Binary Addition 6-2 Representing Signed Numbers 6-3 Addition in the 2 s- Complement System 6-4 Subtraction in the 2 s- Complement

More information

CSCI 2212: Intermediate Programming / C Chapter 15

CSCI 2212: Intermediate Programming / C Chapter 15 ... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25 ... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

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

9 abcd = dcba b + 90c = c + 10b b = 10c.

9 abcd = dcba b + 90c = c + 10b b = 10c. In this session, we ll learn how to solve problems related to place value. This is one of the fundamental concepts in arithmetic, something every elementary and middle school mathematics teacher should

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

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

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

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

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR UNIT I Digital Systems: Binary Numbers, Octal, Hexa Decimal and other base numbers, Number base conversions, complements, signed binary numbers, Floating point number representation, binary codes, error

More information

Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 9/11/2013 Textual data processing (Perl) 1 Announcements If you did not get a PIN to enroll, contact Stephanie Meik 2 Outline Perl Basics (continued) Regular Expressions

More information

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 3 Bits and Bytes; Bitwise Operators CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems In everyday life, we humans most often count using decimal or base-10 numbers. In computer science, it

More information

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other Kinds Of Data CHAPTER 3 DATA REPRESENTATION Numbers Integers Unsigned Signed Reals Fixed-Point Floating-Point Binary-Coded Decimal Text ASCII Characters Strings Other Graphics Images Video Audio Numbers

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

following determine whether it is an element, subset or neither of A and

following determine whether it is an element, subset or neither of A and 1. Let A, B, and C be subsets of the universal set U. Draw Venn diagrams illustrating the following: First, we have a square representing the universe U and three circles, one for each of A, B and C. Each

More information

unused unused unused unused unused unused

unused unused unused unused unused unused BCD numbers. In some applications, such as in the financial industry, the errors that can creep in due to converting numbers back and forth between decimal and binary is unacceptable. For these applications

More information

(Refer Slide Time: 01:12)

(Refer Slide Time: 01:12) Internet Technology Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture No #22 PERL Part II We continue with our discussion on the Perl

More information

CS 105 Perl: Modules and Objects

CS 105 Perl: Modules and Objects CS 105 Perl: Modules and Objects February 20, 2013 Agenda Today s lecture is an introduction to Perl modules and objects, but first we will cover a handy feature Perl has for making data structures. Where

More information

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not. What is an INTEGER/NONINTEGER? Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not. What is a REAL/IMAGINARY number? A real number is

More information

Perl. Interview Questions and Answers

Perl. Interview Questions and Answers and Answers Prepared by Abhisek Vyas Document Version 1.0 Team, www.sybaseblog.com 1 of 13 Q. How do you separate executable statements in perl? semi-colons separate executable statements Example: my(

More information

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation Course Schedule CS 221 Computer Architecture Week 3: Information Representation (2) Fall 2001 W1 Sep 11- Sep 14 Introduction W2 Sep 18- Sep 21 Information Representation (1) (Chapter 3) W3 Sep 25- Sep

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

NUMBER SENSE AND OPERATIONS. Competency 0001 Understand the structure of numeration systems and multiple representations of numbers.

NUMBER SENSE AND OPERATIONS. Competency 0001 Understand the structure of numeration systems and multiple representations of numbers. SUBAREA I. NUMBER SENSE AND OPERATIONS Competency 0001 Understand the structure of numeration systems and multiple representations of numbers. Prime numbers are numbers that can only be factored into 1

More information

Chapter 1. Math review. 1.1 Some sets

Chapter 1. Math review. 1.1 Some sets Chapter 1 Math review This book assumes that you understood precalculus when you took it. So you used to know how to do things like factoring polynomials, solving high school geometry problems, using trigonometric

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

Perl Library Functions

Perl Library Functions Perl Library Functions Perl has literally hundreds of functions for all kinds of purposes: file manipulation, database access, network programming, etc. etc. It has an especially rich collection of functions

More information

UNIT 1: INTEGERS Definition Absolute Value of an integer How to compare integers

UNIT 1: INTEGERS Definition Absolute Value of an integer How to compare integers UNIT 1: INTEGERS 1.1. Definition Integers are the set of whole numbers and their opposites. The number line is used to represent integers. This is shown below. The number line goes on forever in both directions.

More information

UNIX Shell Programming

UNIX Shell Programming $!... 5:13 $$ and $!... 5:13.profile File... 7:4 /etc/bashrc... 10:13 /etc/profile... 10:12 /etc/profile File... 7:5 ~/.bash_login... 10:15 ~/.bash_logout... 10:18 ~/.bash_profile... 10:14 ~/.bashrc...

More information

COMPETENCY 1.0 UNDERSTAND THE STRUCTURE OF THE BASE TEN NUMERATION SYSTEM AND NUMBER THEORY

COMPETENCY 1.0 UNDERSTAND THE STRUCTURE OF THE BASE TEN NUMERATION SYSTEM AND NUMBER THEORY SUBAREA I. NUMBERS AND OPERATIONS COMPETENCY.0 UNDERSTAND THE STRUCTURE OF THE BASE TEN NUMERATION SYSTEM AND NUMBER THEORY Skill. Analyze the structure of the base ten number system (e.g., decimal and

More information

The second statement selects character number 1 from and assigns it to.

The second statement selects character number 1 from and assigns it to. Chapter 8 Strings 8.1 A string is a sequence A string is a sequence of characters. You can access the characters one at a time with the bracket operator: The second statement selects character number 1

More information

Pre-Algebra Notes Unit Five: Rational Numbers and Equations

Pre-Algebra Notes Unit Five: Rational Numbers and Equations Pre-Algebra Notes Unit Five: Rational Numbers and Equations Rational Numbers Rational numbers are numbers that can be written as a quotient of two integers. Since decimals are special fractions, all the

More information

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion Types of recursion Readings: none. In this module: learn to use accumulative recursion learn to recognize generative recursion CS 135 Fall 2018 07: Types of recursion 1 Structural vs. general recursion

More information

CIT 590 Homework 6 Fractions

CIT 590 Homework 6 Fractions CIT 590 Homework 6 Fractions Purposes of this assignment: Get you started in Java and Eclipse Get you comfortable using objects in Java Start looking at some common object uses in Java. General Idea of

More information

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal.

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal. Chapter 8 out of 7 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal 8 Matrices Definitions and Basic Operations Matrix algebra is also known

More information

HTML5 and CSS3 More JavaScript Page 1

HTML5 and CSS3 More JavaScript Page 1 HTML5 and CSS3 More JavaScript Page 1 1 HTML5 and CSS3 MORE JAVASCRIPT 3 4 6 7 9 The Math Object The Math object lets the programmer perform built-in mathematical tasks Includes several mathematical methods

More information

ENE 334 Microprocessors

ENE 334 Microprocessors Page 1 ENE 334 Microprocessors Lecture 10: MCS-51: Logical and Arithmetic : Dejwoot KHAWPARISUTH http://webstaff.kmutt.ac.th/~dejwoot.kha/ ENE 334 MCS-51 Logical & Arithmetic Page 2 Logical: Objectives

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

Section 1.2 Fractions

Section 1.2 Fractions Objectives Section 1.2 Fractions Factor and prime factor natural numbers Recognize special fraction forms Multiply and divide fractions Build equivalent fractions Simplify fractions Add and subtract fractions

More information

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators: http://www.tutorialspoint.com/ruby/ruby_operators.htm RUBY OPERATORS Copyright tutorialspoint.com Ruby supports a rich set of operators, as you'd expect from a modern language. Most operators are actually

More information

CS61A Notes Week 13: Interpreters

CS61A Notes Week 13: Interpreters CS61A Notes Week 13: Interpreters Read-Eval Loop Unlike Python, the result of evaluating an expression is not automatically printed. Instead, Logo complains if the value of any top-level expression is

More information

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM COURSE / CODE DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) NUMBER SYSTEM A considerable subset of digital systems deals with arithmetic operations. To understand the

More information

Prepared by Sa diyya Hendrickson. Package Summary

Prepared by Sa diyya Hendrickson. Package Summary Introduction Prepared by Sa diyya Hendrickson Name: Date: Package Summary Exponent Form and Basic Properties Order of Operations Using Divisibility Rules Finding Factors and Common Factors Primes, Prime

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

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1.1 Introduction Given that digital logic and memory devices are based on two electrical states (on and off), it is natural to use a number

More information

MA 1128: Lecture 02 1/22/2018

MA 1128: Lecture 02 1/22/2018 MA 1128: Lecture 02 1/22/2018 Exponents Scientific Notation 1 Exponents Exponents are used to indicate how many copies of a number are to be multiplied together. For example, I like to deal with the signs

More information

We would like to find the value of the right-hand side of this equation. We know that

We would like to find the value of the right-hand side of this equation. We know that CS 61 Scribe Notes Computer Arithmetic II, Structured Data, Memory Layout (Th 9/13) Renzo Lucioni, Weishen Mead, Kat Zhou Unsigned Computer Arithmetic II Consider the following: We would like to find the

More information

DESIGN OF A COMPOSITE ARITHMETIC UNIT FOR RATIONAL NUMBERS

DESIGN OF A COMPOSITE ARITHMETIC UNIT FOR RATIONAL NUMBERS DESIGN OF A COMPOSITE ARITHMETIC UNIT FOR RATIONAL NUMBERS Tomasz Pinkiewicz, Neville Holmes, and Tariq Jamil School of Computing University of Tasmania Launceston, Tasmania 7250 AUSTRALIA Abstract: As

More information

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

More information

Divisibility Rules and Their Explanations

Divisibility Rules and Their Explanations Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although

More information

Administrivia. Simple data types

Administrivia. Simple data types Administrivia Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Project 0 was due today Reminder:

More information

print inf + 42,"\n"; print NaN * 7,"\n"; print hex("0x "),"\n"; # Perl v or later

print inf + 42,\n; print NaN * 7,\n; print hex(0x ),\n; # Perl v or later NAME bigint - Transparent BigInteger support for Perl SYNOPSIS use bigint; $x = 2 + 4.5,"\n"; # BigInt 6 print 2 ** 512,"\n"; # really is what you think it is print inf + 42,"\n"; # inf print NaN * 7,"\n";

More information

Learning the Binary System

Learning the Binary System Learning the Binary System www.brainlubeonline.com/counting_on_binary/ Formated to L A TEX: /25/22 Abstract This is a document on the base-2 abstract numerical system, or Binary system. This is a VERY

More information

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python Outline Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer Universität des Saarlandes October 6th, 2009 Outline Outline Today s Topics: 1 More Examples 2 Cool Stuff 3 Text Processing

More information

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007 Perl and Python ESA 2007/2008 Eelco Schatborn eelco@os3.nl 27 September 2007 ESA: Perl Vandaag: 1. Perl introduction 2. Basic Perl: types, variables, statements,... 3. Object Oriented Perl 4. Documentation

More information

0001 Understand the structure of numeration systems and multiple representations of numbers. Example: Factor 30 into prime factors.

0001 Understand the structure of numeration systems and multiple representations of numbers. Example: Factor 30 into prime factors. NUMBER SENSE AND OPERATIONS 0001 Understand the structure of numeration systems and multiple representations of numbers. Prime numbers are numbers that can only be factored into 1 and the number itself.

More information

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1 Types of recursion Readings: none. In this module: a glimpse of non-structural recursion CS 135 Winter 2018 07: Types of recursion 1 Structural vs. general recursion All of the recursion we have done to

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

(Refer Slide Time 6:48)

(Refer Slide Time 6:48) Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 8 Karnaugh Map Minimization using Maxterms We have been taking about

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

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Signed umbers. Sign/Magnitude otation

Signed umbers. Sign/Magnitude otation Signed umbers So far we have discussed unsigned number representations. In particular, we have looked at the binary number system and shorthand methods in representing binary codes. With m binary digits,

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

More information

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers A Big Step Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Copyright 2006 2009 Stewart Weiss What a shell really does Here is the scoop on shells. A shell is a program

More information

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENEL 353 Fall 207 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 207 SN s ENEL 353 Fall 207 Slide Set 5 slide

More information

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi Bash Scripting Alessandro Barenghi Dipartimento di Elettronica, Informazione e Bioingegneria Politecnico di Milano alessandro.barenghi - at - polimi.it April 28, 2015 Introduction The bash command shell

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.

More information

Computer Architecture Set Four. Arithmetic

Computer Architecture Set Four. Arithmetic Computer Architecture Set Four Arithmetic Arithmetic Where we ve been: Performance (seconds, cycles, instructions) Abstractions: Instruction Set Architecture Assembly Language and Machine Language What

More information

CS61, Fall 2012 Midterm Review Section

CS61, Fall 2012 Midterm Review Section CS61, Fall 2012 Midterm Review Section (10/16/2012) Q1: Hexadecimal and Binary Notation - Solve the following equations and put your answers in hex, decimal and binary. Hexadecimal Decimal Binary 15 +

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information