Download Introduction to programming Concepts and tools E2005

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
HLO - Udvikling
[email protected]
Introduction to programming Concepts and tools E2005
Students:
Henrik Loop (loop)
Flemming Thor Hansen (flemmingth)
Assignment no. 3
Link to code:
http://www.itu.dk/people/loop/primeprint.java
http://www.itu.dk/people/loop/primeprintIO.java
http://www.itu.dk/people/flemmingth/Fibonacci.java
http://www.itu.dk/people/flemmingth/Fibonaccivariation.java
http://www.itu.dk/people/flemmingth/FibonacciNonRecursiv.java
http://www.itu.dk/people/flemmingth/stringLengthLoopProgram.java
http://www.itu.dk/people/flemmingth/stringLengthRecursionProgram.java
Question 1:
(a) Please find our code on http://www.itu.dk/people/loop/primeprint.java
(b) Please find our on http://www.itu.dk/people/loop/primeprintIO.java
Question 2:
(a) Please find our code at http://www.itu.dk/people/flemmingth/Fibonacci.java
Paper copy of method:
//
//
//
//
//
//
//
//
//
static int fibRec(int x) {
At first the 3 special cases are handled
f(1) = 1
f(2) = 1
and to ensure that recursion
will always return an answer
f(x) = 1 if x <= 0
//System.out.print("*");
if (x <= 0)
return 0;
if (x == 1)
return 1;
if (x == 2)
return 1;
The recursive method gives a very
simple calculation equal to the
definition
840983987
Oprettet af HLO
Side 1
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
return fibRec(x-1) + fibRec(x-2);
}
(b) Please find our code at http://www.itu.dk/people/flemmingth/Fibonaccivariation.java
import tio.*;
public class Fibonacci {
// ***************************************************************
// This program will print the 20 first Fibonacci numbers
//
// The Fibonacci numbers are definded like this
//
// f(1) = 1
// f(2) = 2
// f(n) = f(n-1) + f(n-2)
public static void main (String[] args){
//
Initialize loop to execute 20 times
for (int i=1; i <= 20; i++) {
Each time the fibRec method is called to print the
//
number
System.out.println("The " + i + ". Fibonacci
number is " + fibRec(i));
};
}
//
//
//
//
//
//
//
//
//
//
//
Our genious method for calculating
Fibonacci numbers using recursion :-)0.08"
static int fibRec(int x) {
At first the 3 special cases are handled
f(1) = 1
f(2) = 1
and to ensure that recursion
will always return an answer
f(x) = 1 if x <= 0
//System.out.print("*");
if (x <= 0)
return 0;0.08"
if (x == 1)
return 1;
if (x == 2)
return 1;
The recursive method gives a very
simple calculation equal to the
definition
return fibRec(x-1) + fibRec(x-2);
}
}
840983987
Oprettet af HLO
Side 2
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
Sample output:
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
The
1. Fibonacci number is 1
2. Fibonacci number is 1
3. Fibonacci number is 2
4. Fibonacci number is 3
5. Fibonacci number is 5
6. Fibonacci number is 8
7. Fibonacci number is 13
8. Fibonacci number is 21
9. Fibonacci number is 34
10. Fibonacci number is 55
11. Fibonacci number is 89
12. Fibonacci number is 144
13. Fibonacci number is 233
14. Fibonacci number is 377
15. Fibonacci number is 610
16. Fibonacci number is 987
17. Fibonacci number is 1597
18. Fibonacci number is 2584
19. Fibonacci number is 4181
20. Fibonacci number is 6765
(c) f(1) requires 1 method call
f(2) requires 1 method call
f(3) requires 3 method calls (1+1+1)
f(4) requires 5 method calls (1+3+1)
f(5) requires 9 method calls (1+5+3)
...
and general
f(n) requires 1 + f(n-1) + f(n-2) method calls
(d) As seen in the calculations above the number of calls is also a Fibonacci like series. The
difference is that it adds 1 call = the primary call to the formula. As the formula for the
Fibonacci variation is just one greater than the Fibonacci it completes the proof.
840983987
Oprettet af HLO
Side 3
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
Question 3:
Please find our code here: http://www.itu.dk/people/flemmingth/FibonacciNonRecursiv.java
import tio.*;
public class FibonacciNonRecursiv {
// ***************************************************************
// This program will print the 20 first Fibonacci numbers
//
// The Fibonacci numbers are definded like this
//
// f(1) = 1
// f(2) = 2
// f(n) = f(n-1) + f(n-2)
public static void main (String[] args){
//
Initialize loop to execute 20 times
for (int i=1; i <= 42; i++) {
Each time the fibRec method is called to print the number
System.out.println("The " + i + ". Fibonacci number is " +
//
fibIt(i));
};
}
//
//
Our genious method for calculating
Fibonacci numbers using recursion :-)
static int fibIt(int x) {
int y = 1, z = 1, w = 0 ;
//
//
//
//
//
//
At first the 3 special cases are handled
f(1) = 1
f(2) = 1
and to ensure that recursion Rec
will always return an answer
f(x) = 1 if x <= 0
//System.out.print("*");
if (x <= 0)
return 0;
if (x == 1)
return 1;
if (x == 2)
return 1;
else
for(int i=3; i<=x; i++){
w = y + z;
y = z;
z = w;
}
return w;
}
}
Our method FibIt:
840983987
Oprettet af HLO
Side 4
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
static int fibIt(int x) {
int y = 1, z = 1, w = 0 ;
//
//
//
//
//
//
At first the 3 special cases are handled
f(1) = 1
f(2) = 1
and to ensure that recursion Rec
will always return an answer
f(x) = 1 if x <= 0
//System.out.print("*");
if (x <= 0)
return 0;
if (x == 1)
return 1;
if (x == 2)
return 1;
else
for(int i=3; i<=x; i++){
w = y + z;
y = z;
z = w;
}
return w;
}
Sample output:
[loop@hulk public_html]$ java FibonacciNonRecursiv
The 1. Fibonacci number is 1
The 2. Fibonacci number is 1
The 3. Fibonacci number is 2
The 4. Fibonacci number is 3
The 5. Fibonacci number is 5
The 6. Fibonacci number is 8
The 7. Fibonacci number is 13
The 8. Fibonacci number is 21
The 9. Fibonacci number is 34
The 10. Fibonacci number is 55
The 11. Fibonacci number is 89
The 12. Fibonacci number is 144
The 13. Fibonacci number is 233
The 14. Fibonacci number is 377
The 15. Fibonacci number is 610
The 16. Fibonacci number is 987
The 17. Fibonacci number is 1597
The 18. Fibonacci number is 2584
The 19. Fibonacci number is 4181
The 20. Fibonacci number is 6765
The 21. Fibonacci number is 10946
The 22. Fibonacci number is 17711
The 23. Fibonacci number is 28657
The 24. Fibonacci number is 46368
The 25. Fibonacci number is 75025
The 26. Fibonacci number is 121393
The 27. Fibonacci number is 196418
The 28. Fibonacci number is 317811
The 29. Fibonacci number is 514229
The 30. Fibonacci number is 832040
The 31. Fibonacci number is 1346269
The 32. Fibonacci number is 2178309
The 33. Fibonacci number is 3524578
The 34. Fibonacci number is 5702887
The 35. Fibonacci number is 9227465
The 36. Fibonacci number is 14930352
The 37. Fibonacci number is 24157817
The 38. Fibonacci number is 39088169
The 39. Fibonacci number is 63245986
840983987
Oprettet af HLO
Side 5
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
The 40. Fibonacci number is 102334155
The 41. Fibonacci number is 165580141
The 42. Fibonacci number is 267914296
(c)
Please find the non recursive output above. Below is the output from the program using recursive
method.
[loop@hulk public_html]$ java Fibonacci
The 1. Fibonacci number is 1
The 2. Fibonacci number is 1
The 3. Fibonacci number is 2
The 4. Fibonacci number is 3
The 5. Fibonacci number is 5
The 6. Fibonacci number is 8
The 7. Fibonacci number is 13
The 8. Fibonacci number is 21
The 9. Fibonacci number is 34
The 10. Fibonacci number is 55
The 11. Fibonacci number is 89
The 12. Fibonacci number is 144
The 13. Fibonacci number is 233
The 14. Fibonacci number is 377
The 15. Fibonacci number is 610
The 16. Fibonacci number is 987
The 17. Fibonacci number is 1597
The 18. Fibonacci number is 2584
The 19. Fibonacci number is 4181
The 20. Fibonacci number is 6765
The 21. Fibonacci number is 10946
The 22. Fibonacci number is 17711
The 23. Fibonacci number is 28657
The 24. Fibonacci number is 46368
The 25. Fibonacci number is 75025
The 26. Fibonacci number is 121393
The 27. Fibonacci number is 196418
The 28. Fibonacci number is 317811
The 29. Fibonacci number is 514229
The 30. Fibonacci number is 832040
The 31. Fibonacci number is 1346269
The 32. Fibonacci number is 2178309
The 33. Fibonacci number is 3524578
The 34. Fibonacci number is 5702887
The 35. Fibonacci number is 9227465
The 36. Fibonacci number is 14930352
The 37. Fibonacci number is 24157817
The 38. Fibonacci number is 39088169
The 39. Fibonacci number is 63245986
The 40. Fibonacci number is 102334155
The 41. Fibonacci number is 165580141
The 42. Fibonacci number is 267914296
Our observation of the two scripts is that recursive computing can be expensive when large
amount recursive method calls are been computed.
(d)
The recursive method is rather straight forward to implement as it follows almost directly
from the definition. The iterative method is a bit more complicated to implement, but it has
the advantage of using much less computing recourses because it calculates more directly.
Using the FibRec method the calculation of the 42. Fibonacci number requires several
million method calls resulting in extensive CPU time.
840983987
Oprettet af HLO
Side 6
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
Question 4:
(a)
import tio.*;
// Include special input output package
import java.lang.*; // Input speciel java package
public class stringLengthLoopProgram {
public static void main (String[] args){
String inputText;
System.out.println("Please enter the string you want to measure the
length of");
inputText = Console.in.readLine();
System.out.println("The length of then string '" + inputText + "' is
" + length(inputText) );
}
static int length(String s) {
/*
This algorithm first test to see if the string is empty
If an empty string is put in, it returns zero
If the input string is not empty it bites one
character of at the time until it gets an empty
string.
Each time a bite is taken a counter is incremented
by one to keep track of the length
*/
if (s.equals(""))
// If case of an empty string
return 0;
int counter = 0;
while (!s.equals("")) {
// Still any lettes to
counter++;
increment counter
s = tail(s);
//
letter off
};
return counter;
//
};
//
return zero
remove?
// if so
bite one more
return the length
The method we are suposed to use is given as
static String tail(String s) {
return s.substring(1);
}
}
(b)
import tio.*;
// Include special input output package
import java.lang.*; // Input speciel java package
840983987
Oprettet af HLO
Side 7
06-05-2017
Senest gemt af HLO
HLO - Udvikling
[email protected]
public class stringLengthRecursionProgram {
public static void main (String[] args){
String inputText;
System.out.println("Please enter the string you want to measure the
length of");
inputText = Console.in.readLine();
System.out.println("The length of then string '" + inputText + "' is
" + length(inputText) );
}
static int length(String s) {
/*
This algorithm first tests to see if the string is empty
If it is it returns zero
If the input string is not empty it uses the recursive formula
length = 1 + length(tail)
here tail is the string, with the first letter removed
*/
if (s.equals(""))
// If case of an empty string return zero
return 0;
int len;
len = 1 + length(tail(s));
return len;
};
//
The method we are suposed to use is given as
static String tail(String s) {
return s.substring(1);
};
}
Sample output for both programs:
Please enter the string you want to measure the length of
Flemming
The length of then string 'Flemming' is 8
840983987
Oprettet af HLO
Side 8
06-05-2017
Senest gemt af HLO
Related documents