<<

Java, Lesson 2 38

Some Problems Exercise. Revise your multiplication program from the last problem set to accept an argument m. Print out the addition and multiplication tables of modulo m. [Hint: Use the remainder operator %.] Exercise. The game “FizzBuzz” is played as follows. Players count o↵ positive integers, saying aloud the next when it is their turn unless the number is divisible by 5 or by 7. If the next number is divisible by 5, the player says “fizz”. If the next number is divisible by 7, the player says “buzz”. If the next number is divisible by both 5 and 7, the player says “fizzbuzz”. Write a program that plays “fizzbuzz” with itself. [Hint: use the remainder operator %.] Exercise. Write a program that takes an input argument consisting of one integer and finds the prime factorization of that number. [Hint: You do not need to check whether any number is prime, because smallest of any number must be prime. Do you see why? Formulate a theorem about the smallest nontrivial positive divisor of any integer. (Nontrivial means “not equal to one”.) Exercise. Modify the previous program so that it loops through all the integers from 1 to 1000 and finds the prime factorization of each one. Exercise.

1. Write a program that takes an input argument consisting of one integer and computes the sum of the digits of that number.

2. Extend your program to decide whether the input integer is divisible by 3 or 9.

3. Extend your program to decide whether the input integer is divisible by 11.

[Hint: What do you get if you compute a%10 and a/10?] Exercise. Write a program that takes a single string as input and counts the number of vowels in the string. [Hint: Use the charAt() method.] Exercise. Write a program that displays the steps of the Four Numbers Game played with integers. The initial four integers should be given as command line inputs to the program.

Copyright c 2019, 2020 by Walter Carlip and the University of Chicago Young Scholars Program Java, Lesson 2 39

Exercise. Universal Product Codes (UPC-A) consist of twelve digits. The twelfth digit is a check-digit that is computed from the other eleven digits as follows:

1. Add the digits in the odd-numbered positions and multiply by three.

2. Add the digits in the even-numbered positions to the result.

3. Find the result modulo 10.

4. If the result is not zero, subtract the result from ten.

Write a program that takes an eleven digit integer as input and produces a 12 digit integer by appending the correct check-digit to the original number. [Hint: What do you get if you compute a/10 followed by a%10?] Exercise. Write a program that takes a String as input and computes the “value” of the String. In this problem an “a” is worth one cent, a “b” two cents, a “c” three cents, etc. [Hint: Use the charAt() method. Decide how you want to handle letters not in the lower case alphabet and program accordingly.] Exercise.

1. Write a program that takes a String and an integer as input and computes the code word obtained using a Caesar Cipher with shift given by the input integer. [Hint: Write the program first using only lower case letters, then expand to use upper and lower case. Of course, you will need a loop and the charAt() method.]

2. Modify your program to take a String and two integers as input and compute the String obtained by applying an ane code using the two input integers.

Exercise. Write a program that takes an integer n as an input argument and computes how many ways n can be written as the di↵erence of two squares, that is, find every pair of integers s and t with the property that n = s2 t2. Which numbers can be written as the di↵erence of squares in only one way? Do you see a pattern?

Copyright c 2019, 2020 by Walter Carlip and the University of Chicago Young Scholars Program Java, Lesson 2 40

Exercise. If the sequence of Fibonacci numbers is reduced modulo n (for some integer n), does the sequence eventually repeat?

1. Write a program that computes an array of Fibonacci numbers modulo n and determine whether the answer to the question is yes or no for several examples. 2. For cases in which the sequences does repeat, compute one complete cycle of the sequence (that is, the part of the sequence that repeats). 3. Alter your program to compute the number of times that the integer 0 appears on one cycle of the Fibonacci numbers modulo n (for various choices of n).

Exercise. The ratios of adjacent Fibonacci numbers provide a good ap- proximation of the Golden Ratio. Write a program that computes the Fi- bonacci numbers F1,F2,F3,...,Fn,... and prints out the ratios F2/F1,F3/F2,...,Fn/Fn 1,.... [Hint: Store the ratios in a variable of type double.] Exercise. A Hilbert prime is an integer of the form 4n + 1 (a ) that is not divisible by any smaller Hilbert number other than 1. Write a program that finds all of the Hilbert primes less than a value input by the user. Exercise. Write a program that computes the day of the week when given the year, month, and day as input. Use the following formula: w = d + 26(m +3)/10 + y + y/4 + c/4 +5c 1(mod7), b c b c b c where w is the day of the week (0 = Sunday, through 6 = Saturday); • d is the day of the month; • m is the shifted month (1 = March,through12=February); • y is the last two digits of the year (e.g., 14 for this year), with January • and February considered part of the previous year; c is the century (e.g., 20 for this century). • [Hint: In Java, when working with positive integers, integer division a/b gives the correct quotient, which is the same as a/b for real numbers!] b c

Copyright c 2019, 2020 by Walter Carlip and the University of Chicago Young Scholars Program Java, Lesson 2 41

Exercise. If you multiply the first n primes together and add one, the resulting number En is called the n-th ,becauseoftheroleit plays in Euclid’s proof that there are infinitely many primes. For example, the third Euclid number is

E =2 3 5+1=31. 5 · · The n-th Euclid number always has a “new” prime factor, that is, a prime factor larger than the each of the first n primes used to create En. Are the Euclid numbers themselves always prime? Are they ever prime? Write a Java program that uses your prime testing method to compute each of the first n Euclid numbers and determine whether or not they are prime. Your program should take the integer n as an input argument. Exercise. Aprimetripleisasetofthreeintegers(a, a +2,a+4)allof which are prime. There is only one prime triple, namely (3, 5, 7). What happens if we consider instead triples of integers (a, a +2,a+6),advancing the third number in the triple to the next odd number? Are there any triples of this sort consisting of three prime numbers? If so, how many? Is there more than one such triple? Write a Java program that examines such triples and prints out the triple if it consists of three prime numbers. Your program should take one input (the largest value to use for a)anduseyourbestprimetestingmethod. Exercise. The original Josephus problem asked the following question. Forty-one people sit in a circle in positions labeled from 1 to 41. Every third person in the circle is executed, until only two remain. What positions should you and your best friend sit in to be spared?

Write a program that determines the positions of the two people who • are spared. Modify your program to print out in order the positions of the people • executed. Modify your program to include a method that returns an array con- • taining in the correct order the positions of the people executed and a main method that prints out the returned array. Modify your program to take two inputs: the number n of people sitting • in the circle and the the step m between executed people (i.e., every m-th person is executed).

Copyright c 2019, 2020 by Walter Carlip and the University of Chicago Young Scholars Program Java, Lesson 2 42

n Exercise. A Mersenne number is an integer of the form Mn =2 1, that is, an integer that is one less than a . If the Mersenne number Mn is prime, then Mn is called . Write a program that takes an integer n as input and computes and factors the corresponding Mersenne number Mn. What are the limitations of your program? Can you find any Mersenne primes? (You will revisit this program when you can perform arithmetic with larger integers.) Exercise. Integers n that are themselves not prime, but which share spe- cific properties with all primes are interesting. In particular, according to p 1 Fermat’s Theorem, if p is a prime, then 2 1isdivisiblebyp.Composite n 1 integers n that satisfy this property, i.e., 2 1isdivisiblebyn are called (or sometimes base 2 pseudoprimes or Poulet numbers. Write a program that searches for numbers n with the properties that

1. n is not prime, and

n 1 2. 2 1isdivisiblebyn. Your program should take the highest value to evaluate as its input ar- gument, and print out to the screen each integer n. Write a second version of your program that counts the number of pseu- doprimes below its input argument. Exercise. Aprimenumberp is called a if it is prime and 2 p 1 p divides 2 1. Write a program that searches for Wieferich primes blow the integer given as an input argument. Are there any below 1000? Any below 5000?

Exercise. The Ulam Numbers un,forn =1, 2, 3,... are defined as follows. We specify that u1 =1andu2 =2.Foreachsuccessiveintegerm, m>2, this integer is an if and only if it can be written uniquely as the sum of two distinct (i.e., di↵erent) Ulam numbers. Write a program to find the first n Ulam numbers where n is a positive integer. (This is a very challenging problem.)

Copyright c 2019, 2020 by Walter Carlip and the University of Chicago Young Scholars Program