JAVA

2005/10/31

Lab 10-31 (1) product of N positive numbers 修改過

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Untitled1 {
public static void main(String[] args ) throws IOException
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
double a = Double.parseDouble(keyboard.readLine());;
double ans = 1;
while(a>=0)
{
ans = ans * a ;
a = Double.parseDouble(keyboard.readLine());
}
System.out.println(ans);
}
}

Lab 10-31 (1) product of N positive numbers

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Untitled1 {
public static void main(String[] args ) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
double a = 1;
double ans = 1;
while(a>=0)
{
a = Double.parseDouble(keyboard.readLine());
if (a<=0)
break;
ans = ans * a ;
}
System.out.println(ans);
}
}

Fibonacci number(3)

葉序:自然之美


【摘要】大自然中蘊藏許多的奧秘,如果曾經仔細觀察各類植物葉子的生長,你會發現很有趣的現象:葉子在莖或枝上有一定的排列方法,這種排列方法稱為葉序

簡介

大 自然中蘊藏許多的奧秘,如果曾經仔細觀察各類植物葉子的生長,你會發現很有趣的現象:葉子在莖或枝上有一定的排列方法,這 種排列方法稱為葉序(phyllotaxis)。早在西1754年Bonnet已經做了葉序的觀察〔註一〕,不過在這初期的研究,僅限於植物學家所做的觀 察統計。直到十九世紀初期,才開始有人利用數學模型做深入的探討,同時引起了數學家及物理學家對葉序的興趣。

葉 子是植物的重要器官之一,葉原(primordial leaf)由莖芽之生長點(apex)鄰近的外層細胞分裂而成,漸漸向外生長;若莖或枝上每節(node)僅生一葉者,稱為互生葉(alternate leaf),每節著生二葉者,稱為對生葉(oppositive leaf),每節著生三葉或三葉以上者,稱為輪生葉(whorled leaf)。其中互生葉的葉序若仔細觀察,便會發現依照生出次序依次連接各葉之著生點,在莖枝上將形成螺旋線,故稱螺旋葉序(spiral phyllotaxis),本文以下所提到的葉序均指此類。不過在人類的眼睛中所看到的圖形是很明顯的兩組反方向交錯的螺旋線,稱之為 parastichies;其數目記作(m,l),稱為parastichv pair;原因是人類的視覺自然會將每一著生點與其最鄰近的點做連接而形成。從整個葉序的圖形中可以發現兩點吸引人的特徵,如圖一所示。

一、兩組反方向的螺旋線數目幾乎總是等於Fibonacci數列({1,l,2,3,5,8,l3,21,34,55,……})中的兩個連續值〔註二〕。

二、任意兩個前後長出的葉子與生長點的連線所成的夾角稱

之為分離角(divergence angle),許多植物的分離角經過測量,都很接近黃金角(golden angle Φ),Φ=(1-τ)×360°≒137.5°其中

圖二是向日葵果實的頭狀排列,可以很清楚地看到兩組反方向的螺旋線,順時針方向有34條,反時針方向有55條。其分離角十分接近黃金角Φ。除向日葵外,松果如圖三或鳳梨也都很容易觀察。

目 前已有很多的研究者希望能藉由觀察到植物實際的情況,以各種不同的模型重新建構反向雙螺旋線的圖形,當然他們大多集中在 Fibonacci數列,及黃金角兩項特徵上。然而在本文後段,將會提到也有些植物的葉序所呈現出的兩組反方向螺旋線的數目並不是Fibonacci數列 中的數值。在本文中我們將提出兩個重要的問題:

(l)分離角與兩組反方向螺旋線的數目之間是否存在明確的關係?

(2)葉序形成如此特殊圖案的原因是什麼?

第 一個問題 Jean在1988年已經做嚴格的探討〔註三〕,這裡我們利用廣義的Fibonacci數列導出分離角與兩組反方向螺旋線的數目之間的關係式。 Schoute於1913年〔註五〕提出所謂抑制機制,當葉原在莖芽頂端附近的分裂組織上形成時,將會對下一個初生葉的形成產生排斥,以至於下一個初生葉 形成的位置會盡量遠離上一個初生葉。基於上述的理論,我們採用Douady與Couder的模型〔註四〕,把葉的生長過程,以物理質點運動方式做模擬;新 的初生葉在半徑為RO的生長點周圍形成,其位置由使系統總位能達到最低的條件來決定,向外生長的速率為指數增加模式〔〕,只要質點之間存在的是一種斥力,不論位能的形態如何,都不會影響模擬的結果。在此模型中,參數g等於間隔期比(plastochrone ratio)〔註六〕的自然對數值,而g的初始情g0決定了反方向的螺旋線的數目,當然也就決定葉序的圖形。我們改變不同的g0模擬得到的結果,可以說明不同的葉序的圖形,在自然界中觀察到的相對機率〔註七〕。

分離角與反方向雙螺旋線數目之關係

假 設植物在某一間隔期比,其反方向雙螺旋線的數目記作(i, j),i表順時針的螺旋線數目,j表逆時針的螺旋線數目(假設 i<j),如圖四所見,黑點代表葉子,旁邊標示的數字說明葉子長出的順序,此時很清楚看到第n+i+j個葉子,與先長出來的葉子中第n+i與n+j個的距 離最近;隨著植物的生長,間隔期比逐漸變小〔註八〕,反方向螺旋線數目將會依照Fibonacci原則,從(i,j)改變至(j, i+j),此時第n+i+j個與第n個的距離大幅減小,而形成第n+i+j個和第n與n+i個的距離最近,如圖五所見。若從在生長點的頂端做觀察,第n+ j與n+i個,正好在第n+i+j個的左右側,同時不會有任意兩個葉子在同一個徑方向上,隨著植物的生長,g漸漸減小,第n+j與n+i個漸漸往第n+i +j個的徑方向上逼近。所以假設葉序的分離角為Φ中則必需滿足

〔iΦ〕<0〔jΦ〕……………(1-a)<〔jΦ〕…………………………(1-a)

〔jΦ〕<0〔iΦ〕……………(1-b)

的條件。此處〔sΦ〕定義成sΦ與其最接近的整數(sΦ)相減值:〔sΦ〕=sΦ-(sΦ)

定義廣義Fibonacci數為G(p,q,n),則

G(p,q,n)=G(p,q,n-l)+G(p,q,n-2)

G(p,q,l)=p

G(p,q,2)= q

p,q為數列開始的前兩項。顯然G(l, l, n)為Fibonacci數;G(1,3,n)著名的Lucas數。利用Fibonacci數的性質,我們可以很容易地證明對整數a, b, p, q若且唯若

∣G(p,q,n+l)×G(a,a+b,n-1)-G(p,q,n)×G(a,a+b,n)∣=1……………………(2)

∣pa-qb∣=1…………………………………………(3)

假設現在 i=G(p,q,n),j= G(p,q,n+l),當g遞減時,n值逐漸變大同時要滿足關係式(1)今定義一個ψn

ψn=G(a,a+b,n)/G(p,q,n+1)+(-1)nεn…(4)

其中εn為一很小的正數,εn<1/g(p,q,2n+2),當 lang="ZH-TW">εn收斂成 0;a與b值由(3)式來決定。利用(2)式得到

【瀏覽原件】

由(5)(6)式可以看出,ψn選擇如(4)式滿足(1)式的條件,若取n→∞則可得到分離角ψ

【瀏覽原件】

其中Fn為Fibonacci數列Fn=G(1,1,n)】。 今若給定p,q值,可以找到任意a,b滿足(3)式,代入(7)式求得分離角ψ。表一所列出來的是一些常見葉序的分離角,表中的〔p,q〕數對為G(p, q,n)的簡單表示法,並做為不同型態葉序之分類,例如葉序的反方向雙螺旋線數目為(13,21)或(21,34)均為G(1,1,n)數列中的連續兩個 值,所以都歸類在〔1,1〕。接著將ψ分別乘以p與q,利用(3)式,得到

【瀏覽原件】

由(8)、(9)知a,b其實是Pψ與qψ最接近的整數,即a=(pψ),b=(qψ),所以(6)式可以改寫成

【瀏覽原件】

此 方程式為Jean所提出的〔3〕,因為只有p,q就可以決定ψ值,所以看起來很簡潔,不像(7)式,尚需引入其它整數a, b。不過在實際的計算上,(10)式並不能很容易的算出ψ,至於我們所提出的(7)式,只要有(3)式輔助求得整數a,b,即可簡單地計算出ψ〔註九〕。

g值的重要性

當 葉序的反方向雙螺旋線形成時,自然決定了分離角的大小。但是螺旋線的數目又是受什麼因素所影響?是遺傳呢?抑或是外在的環 境因素? Douady與Couder的模型引入的唯一參數g,藉由動力模擬的過程,決定了反方向雙螺旋線的數目,當然g值實際上有可能受遺傳因子或環境因素所支 配。

在植物的生長過程中,g會漸漸減小,最後等於零,也就是說停止了生長。至於g的初始值以及遞減的 速率,在模擬時是兩項很重要 的因素,前者可能受遺傳特徵所支配;而後者受到環境條件影響,譬如養分的吸收與日照的程度。我們發現g的初始值,直接決定了反方向雙螺旋線的數目與分離角 的大小。

我們在二維平面上做模擬葉序的形成,每一個新生的初生葉,在一個半徑很小的圓周上形成,接著沿著徑方向以與圓心的距離成正比的速度移動,新生的葉原形成的位置,則由最低總位能的條件決定,我們採用1/d3的 能量定律,d為兩個葉子之間的距離,至於其它斥力的能量形態,也得到相同的結果。我們給定一個g值的遞減速率(每步0.001),掃瞄 g的初始值由O至l,記錄最後g→0時,分離角的大小,確定反方向雙螺旋線的數目,結果分離角與表一所列相同。進一步將分離角對g的初始值g0畫圖,g值遞減的速率分別為每步 0.00025與 0.00010,得到圖六與圖七。在g0>0.21時,分離角等於黃金角137.5°,而且相對應的反方向雙螺旋線數目正好為 Fibonacci數列中的連續兩項值。0.11<g0<0.21反方向雙螺旋線數目論Lucas數列中的連續兩項值。g0<0.11時,某些g0所對應的反方向雙螺旋線數目落在其它廣義Fibonacci數列中。圖六遞減速率為0.00025,在g0<0.04的範圍得不到收斂值;而在圖七中,g遞減速率為 0.00010,在0.03<g0<0.04仍可以見到收斂值的存在;一般而言,遞減速率愈小,愈能看出更多的結構。

對g0>0.04的所有值,我們得到的結果只有〔l,2〕,〔l,3〕,〔l,4〕,〔2,5〕,2〔1,2〕,2〔l,3〕.及3〔l,2〕等圖形。另外,在0.03<g0<0.04的範圍出現了〔3,8〕,我們希望在g0<0.03的範圍,能得到更多樣的圖形。譬如〔l,5〕,〔l,6〕,2〔1,4〕等等;若g0是 隨機產生,我們的結果顯示〔l,2〕是最有可能發生,〔l,3〕與2〔l,2〕是其次有可能發生的。表二是 Jean 〔註十〕收集超過 650種植物 12750的樣本中,8種不同圖形出現的機率,與我們的結果頗為一致,唯有〔l,3〕與2〔l,2〕出現頻率的百分比次序相反。至於〔3,8〕與3〔l, 2〕,在表二,雖然沒有列出,不過事實上已經有人提出觀察到的報告〔註十一〕。

結語

我 們找到了反向 雙螺旋線數目與分離角的關係。螺旋線數目是依照Fibonacci數列的規律而改變,以致產生一個有限的分離角。在Douady與Couder的模型架構 上,我們模擬的結果顯示,g的初始值是一個影響螺旋線數目很重要的因素。更重要一點是從自然界上所觀察到不同螺旋狀葉序發生的頻率與模擬的結果大約一致。 另外,對於輪生葉之排列,我們也非常有興趣。直至目前為止,尚無令人信服的說法發表〔註十二〕。不過Douady與Couder卻有一個新的想法,就是加 入一項能量的閥值(threshold)來控制新生葉的形成時機,而這項研究工作正持續進行中。

廖思善、劉瑞堂任教於中興大學物理系

本文改寫自我們發表在Physical Review的文章,參註七。

註釋

一. R. V. Jean, Phyllotaxis: A Systemic Study in Plant Morphogenesis(Cambridge University Press, Cambridge,1994).

二. Fibonacci數列是一個非常有趣且值得研究的數列,最早出現在12世紀義大利人Fibonacci的著作裡。Fiboncci數列的第一及第二個數都為1,之後的每一個數字都等於其前兩個數字和。即令Fn為Fibonacci數列的第n個數,則Fn=Fn-1+Fn-2。Fibonacci數列具有許多有趣的性質,其中之一是

有興趣的讀者請參考網頁

http://www.ee.surrey.ac.uk/Personal/R.knott/Fibonacci/fib.html。

三. R. V. Jean, J. Number Theory 29,2O6(1988)

四. J. C. Schoute, Recl. Trav. Bot. Neel., 10.(1913).

五. S. Douady and Y. Couder, Pev. Rev. Lett. 68,2098(1992).

六.在植物學上間隔期比R定義為rn/rn+1,rn第n個初生葉與生長點頂端的距離,根據觀察植物的間膈期比R是逐漸遞減。g參數定義為:eg=R即g=lnR

七. S. S. Liaw, Phys. Rev.l(l998).

八. G. J. Mitchison, Science 196,27O(1977).

九.給定p,q,有無限多組a,b滿足式(3),但是所有滿足式(3)的a,b,根據式(7)計算出來的角度都是同一個角,因此我們只要找到最簡單的一組a,b滿足式(3)即可。

十. See Phyllotaxis: A Systemic Study in Plant Morphogenesis, ch. 7.

十一. B. Zagorska-Marek, Can. J. Bot. 63,1844(1985).

十二. See Phvllotaxis: A Systemic Study in Plant Morpogenesis, ch. 8.


出處:http://163.20.91.130/sct/content/1999/00030351/0015.htm





Fibonacci number(2)

Petals on flowers

On many plants, the number of petals is a Fibonacci number:
buttercups have 5 petals; lilies and iris have 3 petals; some delphiniums have 8; corn marigolds have 13 petals; some asters have 21 whereas daisies can be found with 34, 55 or even 89 petals.
The links here are to various flower and plant catalogues:

  • the Dutch Flowerweb's searchable index called Flowerbase.
  • The US Department of Agriculture's Plants Database containing over 1000 images, plant information and searchable database.
fuchsia has 4 sepals
Fuchsia
5 petals on a pink
Pinks
lily
Lily
Mike Bentley - Four Daisies
Daisies
available as a
poster at AllPosters.com
3 petals: lily, iris
Mark Taylor (Australia), a grower of Hemerocallis and Liliums (lilies) points out that although these appear to have 6 petals as shown above, 3 are in fact sepals and 3 are petals. Sepals form the outer protection of the flower when in bud. Mark's Barossa Daylilies web site (opens in a new window) contains many flower pictures where the difference between sepals and petals is clearly visible.
4 petals Very few plants show 4 petals (or sepals) but some, such as the fuchsia above, do. 4 is not a Fibonacci number! We return to this point near the bottom of this page.
5 petals: buttercup, wild rose, larkspur, columbine (aquilegia), pinks (shown above)
The humble buttercup has been bred into a multi-petalled form.
8 petals: delphiniums
13 petals: ragwort, corn marigold, cineraria, some daisies
21 petals: aster, black-eyed susan, chicory
34 petals: plantain, pyrethrum
55, 89 petals: michaelmas daisies, the asteraceae family.
Some species are very precise about the number of petals they have - eg buttercups, but others have petals that are very near those above, with the average being a Fibonacci number. Here is a passion flower (passiflora incarnata) from the back and front:
passion flower from back passion flower from front
Back view:
the 3 sepals that protected the bud are outermost,
then 5 outer green petals followed by an inner layer of 5 more paler green petals
Front view:
the two sets of 5 green petals are outermost,
with an array of purple-and-white stamens (how many?);
in the centre are 5 greenish stamens (T-shaped) and
uppermost in the centre are 3 deep brown carpels and style branches)

Fibonacci number(1)

Fibonacci Rectangles and Shell Spirals

fibspiral fibspiral We can make another picture showing the Fibonacci numbers 1,1,2,3,5,8,13,21,.. if we start with two small squares of size 1 next to each other. On top of both of these draw a square of size 2 (=1+1).

We can now draw a new square - touching both a unit square and the latest square of side 2 - so having sides 3 units long; and then another touching both the 2-square and the 3-square (which has sides of 5 units). We can continue adding squares around the picture, each new square having a side which is as long as the sum of the latest two square's sides. This set of rectangles whose sides are two successive Fibonacci numbers in length and which are composed of squares with sides which are Fibonacci numbers, we will call the Fibonacci Rectangles.

fibspiral2.GIF Here is a spiral drawn in the squares, a quarter of a circle in each square. The spiral is not a true mathematical spiral (since it is made up of fragments which are parts of circles and does not go on getting smaller and smaller) but it is a good approximation to a kind of spiral that does appear often in nature. Such spirals are seen in the shape of shells of snails and sea shells and, as we see later, in the arrangment of seeds on flowering plants too. The spiral-in-the-squares makes a line from the centre of the spiral increase by a factor of the golden number in each square. So points on the spiral are 1.618 times as far from the centre after a quarter-turn. In a whole turn the points on a radius out from the center are 1.6184 = 6.854 times further out than when the curve last crossed the same radial line.

Cundy and Rollett (Mathematical Models, second edition 1961, page 70) say that this spiral occurs in snail-shells and flower-heads referring to D'Arcy Thompson's On Growth and Form probably meaning chapter 6 "The Equiangular Spiral". Here Thompson is talking about a class of spiral with a constant expansion factor along a central line and not just shells with a Phi expansion factor.

Below are images of cross-sections of a Nautilus sea shell. They show the spiral curve of the shell and the internal chambers that the animal using it adds on as it grows. The chambers provide boyancy in the water. Click on the picture to enlarge it in a new window. Draw a line from the center out in any direction and find two places where the shell crosses it so that the shell spiral has gone round just once between them. The outer crossing point will be about 1.6 times as far from the centre as the next inner point on the line where the shell crosses it. This shows that the shell has grown by a factor of the golden ratio in one turn.
On the poster shown here, this factor varies from 1.6 to 1.9 and may be due to the shell not being cut exactly along a central plane to produce the cross-section.

Here are some more posters available from AllPosters.com that are great for your study wall or classroom or to go with a science project. Click on the pictures to enlarge them in a new window.


Nautilus poster
Nautilus
Wampler, Sondra
Buy this Art Print at AllPosters.com
Nautilus Shell poster
Nautilus Shell
Myers, Bert
Buy this Art Print at AllPosters.com
Nautilus poster
Nautilus
Schenck, Deborah
Buy this Art Print at AllPosters.com

The curve of this shell is called Equiangular or Logarithmic spirals and are common in nature, though the 'growth factor' may not always be the golden ratio.

Homework 10-24 (1) Lab exponential

import javax.swing.JOptionPane;
public class Exponential
{
public static void main(String[] args)
{
String inputx=JOptionPane.showInputDialog("Enter a number x of exp(x)");
int x=Integer.parseInt(inputx);
String inputn=JOptionPane.showInputDialog("Enter a number n of n!");
int n=Integer.parseInt(inputn);
double i,j,k,a,b,sum=0;
for(i=0;i<=n;i++) { a=1; for(j=i;j>0;j--)
a*=j;
b=1;
for(k=i;k>0;k--)
b*=x;
sum+=(b/a);
}
JOptionPane.showMessageDialog(null,"exp(x) = "+sum);
}
}

2005/10/24

Fibonacci_number


Lab Max-Min

Lab If-Else (2)

package lab_if;
import javax.swing.JOptionPane;

public class Lab_If_Else {
public Lab_If_Else() {
}
public static void main(String[] args) {
String myString = JOptionPane.showInputDialog("Enter a number n = ");
int n = Integer.parseInt(myString);
if (n <= 0)
System.out.println("n<0,n=" + n);
else if( n >= 0 && n < 100 )
System.out.println("0<=n<100,n=" + n);
else if (100 <= n) {
System.out.println("大於100,n=" + n);
}
}
}

Homework 10-17 Project 2.05 (1st ed.)

package homework;
import java.io.*;
import javax.swing.JOptionPane;

public class Homework_10_17 {
public Homework_10_17() {
}
public static void main(String[] args){
String PriceString=JOptionPane.showInputDialog("Enter price of item\n"+
"(from 25 cents to a dollar, in 5-cent increments):");

int price=Integer.parseInt(PriceString);

System.out.println("You bought an item for " + price +
" cents and gave me a dollar,\n"+"so your change is");

price = 100 - price;
int quarter=0,dime=0,nickel=0;

while(price >= 25) {
quarter = price / 25;
price = price - 25 * quarter;
}
while(price>=10 &&amp;amp;amp;amp;amp; price <25){ dime =" price" price =" price">=5 && price <10){ nickel =" price" price =" price" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger/3473/1585/1600/10_17_2_1.jpg">

Homework 10-17 Project 2.03 (1st ed.)




2005/10/18

Lab If-Else

package ifelse;

public class ifelse {
public static void main(String[] args)
{
double netIncome=30000,tax,fivePercentTax,tenPercentTax;
if(netIncome <= 15000)
tax=0;
else if((netIncome > 15000)&&(netIncome <= 30000))
tax=(0.05*(netIncome-15000));
else
{
fivePercentTax=0.05*15000;
tenPercentTax=0.10*(netIncome-30000);
tax=(fivePercentTax+tenPercentTax);
}
System.out.println("Tax due = "+tax);
}
}

BufferedReaderDemo

package bufferedreaderdemo;
/*import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;*/
import java.io.*;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter number of pods:");
String podString = keyboard.readLine();
int Pods = Integer.parseInt(podString);

System.out.println("Enter number of peas in a pod:");
String peaString = keyboard.readLine();
int peasPerPod = Integer.parseInt(peaString);

int totalPeas = Pods * peasPerPod;

System.out.print(Pods + "pods and");
System.out.println(peasPerPod + "peas per pod.");
System.out.println("The total number of peas =" + totalPeas);
}
}



java JOptionpane

package joptionpane;
import javax.swing.JOptionPane;

public class JOptionPane {
public static void main(String[] args)
{
String myString=JOptionPane.showInputDialog("Enter a number");
int myNumber = Integer.parseInt(myString);
System.out.println("The number is "+ myNumber);
System.exit(0);
}

}

2005/10/04

Lab 10-03-2005

2005/10/02

JAVA Homework 3