package com.company;
public class EuclideanAlgorithm {
/**
* 歐幾里得算法求最大公約數(shù)
* 也叫輾轉(zhuǎn)相除法
* 這是基于數(shù)學(xué)原理
* 兩個(gè)整數(shù)的最大公約數(shù)等于其中較小的數(shù)和兩數(shù)的差的最大公約數(shù)
* 實(shí)現(xiàn)的
* @param one
* @param another
*/
static public void euclidean(int one,int another) {
if (one <= 0 || another <= 0) {
System.out.println("參數(shù)不合法");
return;
}
//確保one >= another
if (one < another) {
int tempVariable = another;
another = one;
one = tempVariable;
}
int reminder = one % another;
while (reminder != 0) {
one = another;
another = reminder;
reminder = one % another;
}
System.out.println("最大公約數(shù)是 : " + another);
}
/**
* 求最小公倍數(shù)的方法
* 現(xiàn)在假設(shè)最大公約數(shù)已知為c。
* 這2個(gè)數(shù)為x和y
* 那么x=ac延塑,y=bc幢竹。
* 其中a和b沒(méi)有任何公約數(shù)了
* 根據(jù)小學(xué)求公約數(shù)的方法來(lái)計(jì)算。
* 則最小公倍數(shù)為abc=acb=xb=xbc/c=xy/c
* 最后得出結(jié)論
* 最小公倍數(shù)是xy/c
* @param one
* @param another
*/
static public void minCommonultiple(int one,int another) {
if (one <= 0 || another <= 0) {
System.out.println("參數(shù)不合法");
return;
}
int oneVariable = one;
int anotherVariable = another;
//確保oneVariable >= anotherVariable
if (oneVariable < anotherVariable) {
int tempVariable = anotherVariable;
anotherVariable = oneVariable;
oneVariable = tempVariable;
}
int reminder = oneVariable % anotherVariable;
while (reminder != 0) {
oneVariable = anotherVariable;
anotherVariable = reminder;
reminder = oneVariable % anotherVariable;
}
System.out.println("最小公倍數(shù)是 : " + one * another / anotherVariable);
}
}
輾轉(zhuǎn)相除法
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)讶踪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)芯侥,“玉大人,你說(shuō)我怎么就攤上這事乳讥≈椋” “怎么了廓俭?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)物赶。 經(jīng)常有香客問(wèn)我白指,道長(zhǎng)留晚,這世上最難降的妖魔是什么酵紫? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮错维,結(jié)果婚禮上奖地,老公的妹妹穿的比我還像新娘。我一直安慰自己赋焕,他們只是感情好参歹,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著隆判,像睡著了一般犬庇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上侨嘀,一...
- 那天臭挽,我揣著相機(jī)與錄音,去河邊找鬼咬腕。 笑死欢峰,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涨共。 我是一名探鬼主播纽帖,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼举反!你這毒婦竟也來(lái)了懊直?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤火鼻,失蹤者是張志新(化名)和其女友劉穎吹截,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體凝危,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡波俄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蛾默。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片懦铺。...
- 正文 年R本政府宣布急前,位于F島的核電站醒陆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏裆针。R本人自食惡果不足惜刨摩,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望世吨。 院中可真熱鬧澡刹,春花似錦、人聲如沸耘婚。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)沐祷。三九已至嚷闭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赖临,已是汗流浹背胞锰。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像色乾,于是被迫代替她去往敵國(guó)和親誊册。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 輾轉(zhuǎn)相除法嘲碱, 又名歐幾里德算法(Euclidean algorithm)乃求兩個(gè)正整數(shù)之最大公約數(shù)的算法。 算法描...
- //輾轉(zhuǎn)相除法 int num1 = 48; int num2 = 20; int n1 = num1; //n1...
- 本意是早起枫攀,卻沒(méi)能。本意是拒絕塘匣,卻幫忙脓豪。 陪M小弟去郵局發(fā)了個(gè)邀請(qǐng)函回來(lái)巷帝,往床上一座忌卤,迅速干掉了兩個(gè)香蕉,后...
- 20170306 23:00-23:20 首次 1.5歲楞泼。 2.喜歡驰徊。安靜、乖巧堕阔、聰明棍厂、幹凈清爽。和小時(shí)候的兒子好...