求黃金分割比小數點後無限位(大資料運算,Go+Java語言實現)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

求黃金分割比小數點後無限位(Go+Java語言實現)

理論上給出的代碼可以精確無限位,但事實上太精確的資料對人類是無效的。
下面的代碼是否已經精確到了小數點後2000位精度未測試,但1000多位精度是正確的,只需要調整程式中的常量可以精確到你想要精確的位元。

這是運行時求得的2000位:(CSND不會自動換行)

0.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664780915884607499887124007652170575179788341662562494075890697040002812104276217711177780531531714101170466659914669798731761356006708748071013179523689427521948435305678300228785699782977834784587822891109762500302696156170025046433824377648610283831268330372429267526311653392473167111211588186385133162038400522216579128667529465490681131715993432359734949850904094762132229810172610705961164562990981629055520852479035240602017279974717534277759277862561943208275051312181562855122248093947123414517022373580577278616008688382952304592647878017889921990270776903895321968198615143780314997411069260886742962267575605231727775203536139362107673893764556060605921658946675955190040055590895022953094231248235521221241544400647034056573479766397239494994658457887303962309037503399385621024236902513868041457799569812244574717803417312645322041639723213404444948730231541767689375210306873788034417009395440962795589867872320951242689355730970450959568440175551988192180206405290551893494759260073485228210108819464454422231889131929468962200230144377026992300780308526118075451928877050210968424936271359251876077788466583615023891349333312231053392321362431926372891067050339928226526355620902979864247275977256550861548754357482647181414512700060238901620777322449943530889990950168032811219432048196438767586331479857191139781539780747615077221175082694586393204565209896985556781410696837288405874610337810544439094368358358138113116899385557697548414914453415091295407005019477548616307542264172939468036731980586183391832859913039607201445595044977921207612478564591616083705949878600697018940988640076443617093341727091914336501371

代碼作者:天之,轉載此文請註明出處和原作者

package mainimport ("fmt")type myInt int32const (SIZE    = 120  //數組長度  資料長度小於:120*9TIMES   = 5150 //ab交換次數PRESIZE = 2000 //數位有效位元)/**代碼作者:天之*部落格:http://blog.csdn.net/WAPWO?viewmode=contents */func main() {a, b, tmp, res := make([]myInt, SIZE), make([]myInt, SIZE), make([]myInt, SIZE), make([]byte, PRESIZE)initAB(a, b)gab(a, b, tmp)//printBigNum(a)//printBigNum(b)bigNumDiv(a, b, tmp, res)fmt.Println(res)}/*複製整型數組*/func cpyMyIntArr(d, s []myInt) {for i := 0; i < SIZE; i++ {d[i] = s[i]}}/*大數之和,a=a+b*/func bigNumSum(a, b []myInt) {for i := 0; i < SIZE; i++ {count := a[i] + b[i]//因為已知a[i]和b[i]都是九位元內的if count > 1000000000 {a[i+1] += 1a[i] = count - 1000000000} else {a[i] = count}}//如果不存在a[i+1],這時就溢出了,在輸入時需要控制}/*十倍值,a=10*a*/func bigNum10(a, tmp []myInt) {cpyMyIntArr(tmp, a)for i := 0; i < 9; i++ {bigNumSum(a, tmp)}}/*大數之差,a=a-b*/func bigNumDif(a, b, tmp []myInt) int16 {cpyMyIntArr(tmp, a)for i := 0; i < SIZE; i++ {count := a[i] - b[i]if count < 0 {if i < SIZE-1 {a[i+1] -= 1a[i] = 1000000000 + count} else {cpyMyIntArr(a, tmp)return 0}} else {a[i] = count}}for i := SIZE - 1; i >= 0; i-- {if a[i] < 0 {cpyMyIntArr(a, tmp)return 0}}return 1}/*類比除法運算res=b/a,進入運算時b<a*/func bigNumDiv(a, b, tmp []myInt, res []byte) {var count bytefor i := 0; i < PRESIZE; i++ {count = 0for bigNumDif(b, a, tmp) == 1 {count++}res[i] = countbigNum10(b, tmp)}}/*產生比例數,b/a --> a/(b+a)*/func gab(a, b, tmp []myInt) {for i := 0; i < TIMES; i++ {cpyMyIntArr(tmp, a)bigNumSum(a, b)cpyMyIntArr(b, tmp)}}/*初始化ab數組*/func initAB(a, b []myInt) {for i := 1; i < SIZE; i++ {a[i] = 0b[i] = 0}a[0] = 3b[0] = 2}/*列印大資料數組*/func printBigNum(a []myInt) {for i := SIZE - 1; i >= 0; i-- {fmt.Printf("%10d", a[i])}fmt.Println()}

 

import java.math.BigDecimal;public class Demo {static int i=0;public static void main(String args[]){BigDecimal x=new BigDecimal(Double.parseDouble("0.5"));System.out.println(fun(1,x));}static BigDecimal fun(int times,BigDecimal n){//n'=1/(1+n)BigDecimal a=new BigDecimal(Double.parseDouble("1"));if(times>1000){n=a.add( n );return a.divide(n,300,BigDecimal.ROUND_HALF_UP);}else{n=a.add( n );return fun(times+1,a.divide(n,300,BigDecimal.ROUND_HALF_UP));}}}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.