Category Archives: Assignment

Built-in Ex: Day of the Week

Day of the week.
Write a program DayOfWeek_YI.java that takes a date as input and prints the day of the week that date falls on. Your program should take three command-line arguments: m (month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the Gregorian calendar (where / denotes integer division):

Screen Shot 2015-09-29 at 3.07.19 PM

screen-shot-2016-09-20-at-8-49-10-am

For example, on which day of the week was August 2, 1953?

dow-example

Visit edmodo.com to check due dates and to submit your work.

Built-in Ex: Polar Coordinates

Polar coordinates.
Write a program CartesianToPolar_YI.java that converts from Cartesian to polar coordinates. Your program should take two real numbers x and y on the command line and print the polar coordinates r and θ. Use the Java method Math.atan2(y, x), which computes the arctangent value of y/x that is in the range from -π to π.

screen-shot-2016-09-20-at-9-01-32-am

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.

Conditionals: Counting Sundays

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

Counting Sundays

Problem 19

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?