Monthly Archives: October 2020

Conditionals: if if/else flowcharts

New topic: Conditionals
Click on this label:
Screen Shot 2014-10-02 at 10.05.29 AM

Classwork:
Flowcharts: write a short code fragment for each of the following flowcharts:
if and if/else
flowchars-if-else

while and for loops

flowchart-loop

More on conditionals with the slides on this link:

Assignments:

  1. Write a more general and robust version of ComplexRootsQuad.java that prints the roots of the polynomial ax^2 + bx + c, prints real and complex solutions.
  2. What (if anything) is wrong with each of the following statements?
    if (a > b) then c = 0;
    if a > b { c = 0; }
    if (a > b) c = 0;
    if (a > b) c = 0 else b = 0;
  3. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.
  4. Write a program AllEqual_YI.java that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.
  5. Rewrite TenHellos.java to make a program ManyHellos_YI.java that prompts the user for the consecutive lines to print… You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use “st”, “nd”, “rd”, or “th” for printing the ith Hello.Example output:
    * Starting number? 4
    * Ending number? 8

    * 4th Hello
    * 5th Hello
    * 6th Hello
    * 7th Hello
    * 8th Hello

  6. What is the value of m and n after executing the following code?
    int n = 123456789;
    int m = 0;
    while (n != 0) {
       m = (10 * m) + (n % 10);
       n = n / 10;
    }
    
    
  7. What does the following code print out?
    int f = 0, g = 1;
    for (int i = 0; i <= 15; i++) {
       System.out.println(f);
       f = f + g;
       g = f - g;
    }
    
    

A site to check your understanding:


Conditionals: Ex 1-4

 

Classwork:

  1. Write a program YI_FivePerLine.java that, using one for loop and one if statement, prints the integers from 1000 to 2000 with five integers per line. Hint: use the % operator.

  2. Write a program YI_FunctionGrowth.java that prints a table of the values of ln n, n, n ln n, n^2, n^3, and 2^n for n = 16, 32, 64, …, 2048. Use tabs (‘\t’ characters) to line up columns.

Homework:

  1. What is the value of m and n after executing the following code?

int n = 123456789;
int m = 0;
while (n != 0) {
   m = (10 * m) + (n % 10);
   n = n / 10;
}

  1. What does the following code print out?

int f = 0, g = 1;
for (int i = 0; i <= 15; i++) {
   System.out.println(f);
   f = f + g;
   g = f - g;
}

Conditionals: Ex 1-7

Screen Shot 2014-09-23 at 10.00.31 AM

Classwork:
1. Write a more general and robust version of YI_Quadratic.java that prints the roots of the polynomial ax^2 + bx + c, prints an appropriate error message if the discriminant is negative, and behaves appropriately (avoiding division by zero) if a is zero.

  1. What (if anything) is wrong with each of the following statements?
    if (a > b) then c = 0;
    if a > b { c = 0; }
    if (a > b) c = 0;
    if (a > b) c = 0 else b = 0;

  2. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

Homework: Conditionals and loops
4. Write a program YI_AllEqual.java that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.

  1. Rewrite YI_TenHellos.java to make a program YI_Hellos.java that takes the number of lines to print as a command-line argument. You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use “st”, “nd”, “rd”, or “th” for printing the ith Hello.

  2. What is the value of m and n after executing the following code?

int n = 123456789;
int m = 0;
while (n != 0) {
   m = (10 * m) + (n % 10);
   n = n / 10;
}

  1. What does the following code print out?
int f = 0, g = 1;
for (int i = 0; i <= 15; i++) {
   System.out.println(f);
   f = f + g;
   g = f - g;
}

Conditionals: Simple Interest

Screen Shot 2014-09-23 at 10.00.31 AM

Write a program, SimpleInterest_YI.java that uses a while loop to calculate how many years it would take to have at least $1,100 at an interest rate of 1.2% when $1,000 is deposited in a savings bank account. DO NOT USE THE FORMULA.

stack-of-money

Simple interest is money you can earn by initially investing some money (the principal). A percentage (the interest) of the principal is added to the principal, making your initial investment grow!

As part of your program include the calculation done with the formula.

An example of Simple Interest

Conditionals: Sum of Squares Difference

Project Euler Problem 6

screen-shot-2016-10-27-at-8-36-29-am

Sum square difference
The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + … + 10^2 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Conditionals: Even Fib Numbers

screen-shot-2016-10-27-at-8-36-29-am

Project Euler
Even Fibonacci numbers
Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Write a java program, EvenFibPE2_YI.java to solve this problem.

Conditionals: Prime Factorization

 

  1. Draw a flow chart for the Factors.java program. Here is an example of a flowchart with all the shapes your flow chart should have:

Another good flowchart example:

You can draw flowcharts if you create an account here

  1. Tracing 101: Draw (on paper or doc ) a table of values for the variables “factor” and “n” for the following test data:
  • A table for input: 27
  • A table for input: 42
  • A table for input: 29

 
3. Modify Factors.java to print just one copy of each of the prime divisors. ModFactors_YI.java

 
[spoiler title=’Factors.java’]


/******************************************************************************
 *  Compilation:  javac Factors.java
 *  Execution:    java Factors n
 *  
 *  Computes the prime factorization of n using brute force.
 *
 *   % java Factors 81
 *   The prime factorization of 81 is: 3 3 3 3 
 *
 *   % java Factors 168
 *   The prime factorization of 168 is: 2 2 2 3 7
 *
 *   % java Factors 4444444444
 *   The prime factorization of 4444444444 is: 2 2 11 41 271 9091 
 *
 *   % java Factors 4444444444444463
 *   The prime factorization of 4444444444444463 is: 4444444444444463
 * 
 *   % java Factors 10000001400000049
 *   The prime factorization of 10000001400000049 is: 100000007 100000007 
 *
 *   % java Factors 1000000014000000049
 *   The prime factorization of 1000000014000000049 is: 1000000007 1000000007
 *
 *   % java Factors 9201111169755555649
 *   The prime factorization of 9201111169755555649 is: 3033333343 3033333343 
 *
 *   Can use these for timing tests - biggest 3, 6, 9, 12, 15, and 18 digit primes
 *   % java Factors 997
 *   % java Factors 999983
 *   % java Factors 999999937
 *   % java Factors 999999999989
 *   % java Factors 999999999999989
 *   % java Factors 999999999999999989
 *
 *   Remarks
 *   -------
 *   - Tests factor*factor <= n instead of factor <= n for efficiency.
 *
 *   - The last two examples still take a few minutes.
 *
 ******************************************************************************/

public class Factors {

    public static void main(String[] args) { 

        // command-line argument
        long n = Long.parseLong(args[0]);

        System.out.print("The prime factorization of " + n + " is: ");

        // for each potential factor
        for (long factor = 2; factor*factor <= n; factor++) {

            // if factor is a factor of n, repeatedly divide it out
            while (n % factor == 0) {
                System.out.print(factor + " "); 
                n = n / factor;
            }
        }

        // if biggest factor occurs only once, n > 1
        if (n > 1) System.out.println(n);
        else       System.out.println();
    }
}

[/spoiler]

Click on this label:
Screen Shot 2014-10-02 at 10.05.29 AM

Conditionals: The GCD

The GCD
Write a program GCD_YI.java that finds the greatest common divisor of two integers using the Euclidean Algorithm:

if x > y, then if y divides x, the gcd of x and y is y; otherwise, the gcd of x and y is the same as the gcd of x % y and y

On paper do an example (it can be the one from Khan Academy) but showing all the mathematical step but thinking the way you would write it as a program.

screen-shot-2016-10-17-at-12-02-56-pm

 

Conditionals: Sum of Squares

 

screen-shot-2016-10-27-at-8-36-29-am

A positive integer n is called squarefree, if no square of a prime divides n, thus 1, 2, 3, 5, 6, 7, 10, 11 are squarefree, but not 4, 8, 9, 12.

Consider equations of the form: a2 + b2 = N, 0 ≤ a ≤ bab and N integer.

For N=65 there are two solutions:

a=1, b=8 and a=4, b=7.

We call S(N) the sum of the values of a of all solutions of a2 + b2 = N, 0 ≤ a ≤ b, where ab and N  are integers.

Thus S(65) = 1 + 4 = 5.

Find ∑S(N), for all squarefree N only divisible by primes of the form 4k+1 with 4k+1 < 150.