Register
It is currently Sat Jul 26, 2014 3:07 am

fibonacci number


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Nov 25, 2008 1:59 am   
User avatar

Joined: Tue Nov 25, 2008 12:49 am
Posts: 2
Hi there!

Please tell me, why this bold line is necessary ? If i run script without 'let' it doesn't work.

Quote:
#!/usr/local//bin/bash

function fibonacci {

a=0
b=1
i=0

echo
while [ $i -lt 19 ]
do
printf "$a,"
sum=$a+$b
a=$b
let b=$sum
let i++
done
printf "$a\n"

}

fibonacci


Top
 Profile  
 PostPosted: Tue Nov 25, 2008 9:16 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi,

It's because of the line;

Code:
sum=$a+$b


This does not actually do that sum, if you echo $sum right after that line you'll see that sum=number+number rather than the result of the sum. To do arithmetic normally one would use the $((..)) syntax like so;

Code:
sum=$(($a+$b))


I was in fact unaware that let could be used in that way!


Top
 Profile  
 PostPosted: Wed Nov 26, 2008 3:54 am   
User avatar

Joined: Tue Nov 25, 2008 12:49 am
Posts: 2
OK, Thanks


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP