求黄金分割比小数点后无限位(Go+Java语言实现)
理论上给出的代码可以精确无限位,但事实上太精确的数据对人类是无效的。
下面的代码是否已经精确到了小数点后2000位精度未测试,但1000多位精度是正确的,只需要调整程序中的常量可以精确到你想要精确的位数。
这是运行时求得的2000位:(CSND不会自动换行)
0.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664780915884607499887124007652170575179788341662562494075890697040002812104276217711177780531531714101170466659914669798731761356006708748071013179523689427521948435305678300228785699782977834784587822891109762500302696156170025046433824377648610283831268330372429267526311653392473167111211588186385133162038400522216579128667529465490681131715993432359734949850904094762132229810172610705961164562990981629055520852479035240602017279974717534277759277862561943208275051312181562855122248093947123414517022373580577278616008688382952304592647878017889921990270776903895321968198615143780314997411069260886742962267575605231727775203536139362107673893764556060605921658946675955190040055590895022953094231248235521221241544400647034056573479766397239494994658457887303962309037503399385621024236902513868041457799569812244574717803417312645322041639723213404444948730231541767689375210306873788034417009395440962795589867872320951242689355730970450959568440175551988192180206405290551893494759260073485228210108819464454422231889131929468962200230144377026992300780308526118075451928877050210968424936271359251876077788466583615023891349333312231053392321362431926372891067050339928226526355620902979864247275977256550861548754357482647181414512700060238901620777322449943530889990950168032811219432048196438767586331479857191139781539780747615077221175082694586393204565209896985556781410696837288405874610337810544439094368358358138113116899385557697548414914453415091295407005019477548616307542264172939468036731980586183391832859913039607201445595044977921207612478564591616083705949878600697018940988640076443617093341727091914336501371
代码作者:天之,转载此文请注明出处和原作者
package main import ( "fmt" ) type myInt int32 const ( SIZE = 120 //数组长度 数据长度小于:120*9 TIMES = 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] += 1 a[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] -= 1 a[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 byte for i := 0; i < PRESIZE; i++ { count = 0 for bigNumDif(b, a, tmp) == 1 { count++ } res[i] = count bigNum10(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] = 0 b[i] = 0 } a[0] = 3 b[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)); } } }
有疑问加站长微信联系(非本文作者)