Question 2

advertisement
Introduction to Programming - Concepts and Tools
Students names and number:
Marina Dunning, 251261
Jørgen Tetsche, 080867
Jonas T. Petersen, 090276
Assignment
Assignment no. 3, due on the 24 FEB 2005
LINK
http://www.itu.dk/people/jonasp/Assignments/Assignment%203/
Question 1
(a)
/**
* Return true if the argument is a (positive) primes all primes number
* Returns false otherwise
* Author: Marina, Jørgen og Jonas
* Date: 19 February 2005
*/
import tio.*;
public class isPrime {
public static void main (String[] args){
System.out.print("Please enter an integer number: ");
int n = Console.in.readInt();
boolean result = isPrime (n);
System.out.println (result);
}
static boolean isPrime (int n) {
boolean isPrime = true;
int d=2;
while(d<=Math.sqrt(n)) {
if( n%d==0)
isPrime = false;
d++;
}
return isPrime;
}
}
(b)
/**
* Prints all primes below 50
* Author: Marina, Jørgen og Jonas
* Date: 19 February 2005
*/
import tio.*;
public class primesBelow50{
public static void main (String[] args){
System.out.println("Primes below 50: ");
for (int i=2; i<=50; i++) {
boolean result = isPrime (i);
if (result)
System.out.print(i+ ", " );
}
System.out.println();
}
static boolean isPrime (int n) {
boolean isPrime = true;
int d=2;
while(d<=Math.sqrt(n)) {
if( n%d==0)
isPrime = false;
d++;
}
return isPrime;
}
}
(c)
/**
* Asks the user for the bounding value of prime numbers (s)he
* wants to have calculated.
* Author: Marina, Jørgen og Jonas
* Date: 19 February 2005
*/
import tio.*;
public class primesBelowX{
public static void main (String[] args){
System.out.print("Please enter an integer number: ");
int x = Console.in.readInt();
System.out.println("Primes below " + x + ": ");
for (int i=2; i<=x; i++) {
boolean result = isPrime (i);
if (result)
System.out.print(i+ " " );
}
System.out.println();
}
static boolean isPrime (int n) {
boolean isPrime = true;
int d=2;
while(d<=Math.sqrt(n)) {
if( n%d==0)
isPrime = false;
d++;
}
return isPrime;
}
}
Question 2
(a)
public static int fibRec(int n){
if (n<=0)
return 0;
else if ((n==1) || (n==2))
return 1;
else
return (fibRec(n-1) + fibRec(n-2));
}
(b)
import tio.*;
public class fibonacci{
public static void main (String[] args){
System.out.print("Please enter an integer number: ");
int i = Console.in.readInt();
System.out.println("Fibonacci number of " + i + " is: " + fibRec(i));
}
public static int fibRec(int n){
if (n<=0)
return 0;
else if ((n==1) || (n==2))
return 1;
else
return (fibRec(n-1) + fibRec(n-2));
}
}
(c)
To calculate f(1) is used one method call and fibRec(1) returns the value 1
To calculate f(2) is used one method call and fibRec(2) returns the value 1
To calculate f(3) is used 3method calls and fibRec(3) returns the value 2
To calculate f(4) is used 4 method calls and fibRec(4) returns the value 3
To calculate f(5) is used 9 method calls and fibRec(5) returns the value 5
Main calls
fibRec(5) which calls
fibRec(4) which calls
fibRec(3) which calls
fibRec(2)
fibRec(2)
fibRec(3) which calls
fibRec(2)
fibRec(1)
fibRec(1)
(d)
The formula, mc(n) = 1 + mc(n-1) + mc(n-2), show that every time mc(n) is activated it adds 1 and
makes two methods calls to mc(n-1) and mc(n-2).
Thanks to the information that mc(1) = 1 and mc(2) = 1 we know that the last method calls, mc(1)
and mc(2), will return the value 1.
As a result each method call will only add the value 1 to the aggregated result. Hence for input n the
result f(n) = mc(n) will demand mc(n) method calls.
How to test the program
We will test the first if-statement by inserting a negative number and 0 as n.
We will test the second if-statement by inserting 1 and 2 as n.
We will test the recursive statement by inserting values equal to or above 3.
We will furthermore try to insert illegal values such a letters (a, b, c, …) and signs (+, % etc.)
Sample output
The sample output from the two methods, f(n) and mc(n).
N
f(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1.597
2.584
4.181
6.765
10.946
17.711
28.657
46.368
75.025
121.393
196.418
317.811
514.229
832.040
1.346.269
2.178.309
3.524.578
5.702.887
9.227.465
14.930.352
24.157.817
39.088.169
63.245.986
102.334.155
165.580.141
267.914.296
n
mc(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1.219
1.973
3.193
5.167
8.361
13.529
21.891
35.421
57.313
92.735
150.049
242.785
392.835
635.621
1.028.457
1.664.079
2.692.537
4.356.617
7.049.155
11.405.773
18.454.929
29.860.703
48.315.633
78.176.337
126.491.971
204.668.309
331.160.281
535.828.591
Question 3
(a)
A method to find Fibonacci by NOT using recursive calls
public static fibIt(int n) {
old=1
oldst=1
for (int i=1; i<=n; i++) {
resultat=old+oldst
oldst=old
old=resultat
}
}
(b)
The program to find the Fibonacci.:
import tio.*;
public class fibit {
public static void main (String[] args){
int n;
System.out.print("Please enter n in order to find the Fibonacci value
");
n = Console.in.readInt();
System.out.print (fibIt(n));
System.out.println();
}
static int fibIt(int n) {
int old=1;
int oldst=1;
int resultat=1;
for(int i=3;i<=n;i++)
{
resultat=old+oldst;
oldst=old;
old=resultat;
}
return resultat;
}
}
(c)
Output of f(42) is 267.914.296
(d)
Advantages and disadvantages of the recursive method (fibRec)
The recursive method (fibRec) is simple to write and easy to understand (this makes the
implementation stage - where you write the actual code – easier and might make it easier to
understand the code afterwards during the maintenance period). It might also be possible to use the
recursive in some situations where it is not possible or very difficult to use loops instead.
The large number of method calls, which are necessary when you use the recursive method makes it
very expensive/inefficient to run programs that make use of recursive methods. If many users e.g.
activates a program that makes use of recursive method(s) at the same time, it will use lots of server
capacity and might even cause a server break down (these problems might be revealed during the
testing period as unacceptable slow performance of the program).
Advantages and disadvantages of the loop (fibIt)
It is much more efficient to run loops in a program, since you don’t need all the costly method calls.
It might however be a bit more complicated to read and write (the implementation and maybe the
testing period might be a bit more difficult).
Question 4
Test program with loop – solution 1
import tio.*;
import java.lang.*;
public class lenghtOne {
public static void main (String[] args){
int n;
System.out.print("Please enter a word ");
n = Console.in.readLine();
System.out.print (lenght(n));
System.out.println();
}
static int lenght (string l) {
int counter=1;
while(tail(l).equals(””)!=true)
{
l=tail(l);
counter++;
}
return counter;
}
static String tail(String s){
return s.substring(1);}
}
Test program with loop – solution 2
We can make this program compile but not run, apparently due to an error related to java.lang.
import tio.*;
import java.lang.*;
public class length{
public static void main (String[] args){
System.out.print("Please enter word: ");
String s = Console.in.readLine();
System.out.println("The word "+ s +" consist of "+ length(s) +"
characters");
}
static int length (String s){
int c = 0;
while (tail(s)!="") {
s = tail(s);
c++;
}
return c;
}
static String tail(String s){
return s.substring(1);
}
}
Test program with recursion
We cannot make the program run, apparently due to errors in line 13 and 16 (at least).
import tio.*;
import java.lang.*;
public class lengthRec{
public static void main (String[] args){
System.out.print("Please enter word: ");
String s = Console.in.readLine();
System.out.println("The word "+ s +" consist of "+ lengthRec(s) +"
characters");
}
static int lengthRec (String s){
if (String s = "")
return;
else
lengthRec = tail(string s);
return lengthRec;
}
static String tail(String s){
return s.substring(1);
}
}
Download