Worksheet #2
CSC 142 – Computer Programming I
Due Thursday 1/19/2017
Instructions
Follow the steps in the worksheet and implement the program specified using Java. Follow the
homework example and template provided to you to format your worksheet as well. Attach any Java
code that you wrote or modified as a Java file.
Properties of Numbers
In this worksheet, you will use programming and for loops to explore several interesting
properties of numbers. Each question below lists a specific property. Use programming and follow the
instructions to explore the property given in the problem.
Question 1: Sum of Perfect Odd Squares
It is well-known that two perfect squares can be added together to form another perfect square –
these are called Pythagorean triples. Some examples are given here:
2 2 2 2 2 2 2 2 2 2 2 2
3 + 4 =5 , 5 +12 =13 , 6 +8 =10 , 7 +24 =25
However, you will notice that in each example, there is an even number on the left side. Write a
program that will look for two perfect squares, both odd, that will add together to make another perfect
square.
Question 2: Sum of Odd Numbers
Adding up the odd numbers reveals a curious property – the sum of any sequence of odd
numbers, beginning at 1, always gives a perfect square. For example:
2
1+3=2
2
1+3+5+ 7=4
2
1+3+5+ 7+ 9+11+13+15=8
Is this pattern a coincidence for the first few odd numbers? Confirm that this property holds true for the
case of adding N = 10, N = 50, and N = 100 odd numbers together.
(Can you find a shortcut to write the sum of the first 50 odd numbers 1+3+5+ ...+99+101 ?)
Question 3: Decaying Exponential
In mathematics, the natural base e is a very important naturally-occurring number that is
x
related to growth rates. One way to evaluate the function e is to use an infinite series expansion,
2 3
x x x x
e =1+ + + +.. .
1 (2)(1) (3)(2)(1)
Write a program that begins by setting a value for two variables, x and n. The program should then use
a for loop to calculate each of the terms in the series above, and add them all up to approximate the
x
value of e . Java’s Math library will provide you with math functionality required to do this - you
a x
can use the [Link](x,a) function to compute x , and you can use [Link](x) to compute e .
Run your program, using x=3.14159...=π and n=4 , and include this output with your
worksheet submission.