Hackety Hacking Problem Family Tree FibonacciSeries: Difference between revisions
Jump to navigation
Jump to search
(Add category.) |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
Fibonacci Series :- |
Fibonacci Series :- |
||
It is the series which starts with 0 & 1 and after that every no is the sum of previous two numbers in the series.In mathematical form we can say |
It is the series which starts with 0 & 1 and after that every no is the sum of previous two numbers in the series.In mathematical form we can say |
||
for n<=2 |
for n<=2 |
||
Line 12: | Line 13: | ||
for n>2 |
for n>2 |
||
F(n)=F(n-1) + F(n-2) |
|||
The fibonacci series can be used to understand the growth of a Family tree |
|||
== Sample Output == |
== Sample Output == |
||
Line 31: | Line 34: | ||
* Print the output |
* Print the output |
||
== |
== Solution in RUBY == |
||
<pre> |
|||
Will be given soon....... |
|||
# Take input from user |
|||
puts("Enter the Limit of Fibonacci Series " ); |
|||
k=gets.to_i; |
|||
# Initialize the values when the family tree starts , Beginning of Fibonacci Series |
|||
a=0; |
|||
b=1; |
|||
print a ," ", b |
|||
# Use the relation f(n) = f(n-1) + f(n-2) to interpolate the Family tree |
|||
k.times do |
|||
sum=a+b; |
|||
print(" " , sum ); |
|||
a=b; |
|||
b=sum; |
|||
end |
|||
</pre> |
|||
[[Category: Hackety Lesson Plan]] |
Latest revision as of 04:59, 6 April 2008
Problem Statement
Write a program to print the fibonacci series till 'n' no terms.
Fibonacci Series :- It is the series which starts with 0 & 1 and after that every no is the sum of previous two numbers in the series.In mathematical form we can say
for n<=2
F1=0 F2=1
for n>2
F(n)=F(n-1) + F(n-2)
The fibonacci series can be used to understand the growth of a Family tree
Sample Output
Enter the no of terms to be printed : 10 The Fibonacci Series is : 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34
Curriculum Connection
To solve this problem students will
- Take input from user
- Will first check the nature of problem
- Simulates the mathematical function using loops
- Simulation will require nesting of loops
- Print the output
Solution in RUBY
# Take input from user puts("Enter the Limit of Fibonacci Series " ); k=gets.to_i; # Initialize the values when the family tree starts , Beginning of Fibonacci Series a=0; b=1; print a ," ", b # Use the relation f(n) = f(n-1) + f(n-2) to interpolate the Family tree k.times do sum=a+b; print(" " , sum ); a=b; b=sum; end