寻找周期有理数的十进制扩展 [英] finding cycles in decimal expansion of rational numbers

查看:168
本文介绍了寻找周期有理数的十进制扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写打印0(03)输入1和33的程序。 (1/33 = 0.03030303 ....我们使用符号0(03)来表示03重复下去。)

I need to write a program that prints 0.(03) for input 1 and 33. (1/33 = 0.03030303.... We use the notation 0.(03) to denote that 03 repeats indefinitely.)

作为另一实例, 七万分之八千六百三十九= 0.1234(142857)

As another example, 8639/70000 = 0.1234(142857)

我明白了,我需要使用像floyds的算法。但我怎么得到0.0.030303030303而不是0.03030303030304 Java编写的。

I understand, I need to use an algorithm like floyds. But how do I get 0.0.030303030303 instead of 0.03030303030304 in java.

推荐答案

通过这个code,我想你会发现你在找什么:

With this code, I think you'll find what you're looking for:

BigDecimal one = new BigDecimal(1);

BigDecimal thirtyThree = new BigDecimal(33);

//Fix the decimals you want, i.e. 21
MathContext context = new MathContext(21, RoundingMode.DOWN);

BigDecimal result = one.divide(thirtyThree, context);       

System.out.println(result);

这产生了一个结果: 0.0303030303030303030303

This yields the next result: 0.0303030303030303030303

这篇关于寻找周期有理数的十进制扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆