分子为1的分数称为单分数。分母是2到10的单分数用十进制表示如下:
1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.1
其中0.1(6) 表示 0.166666...,因此它又一个长度为1的循环圈。可以看出1/7拥有一个6位的循环圈。
找出小于1000的数字d,1/d 的十进制表示含有最长的循环圈。
思路:(百度来的~~)
如果一个数的质因子全是2和5的话,这个数的倒数是不会无限循环的
如2,4,5,8,10
而一个数把质因子中的2和5除去后,得到一个数,我们称之为“基数”吧
这个数和它的基数的倒数循环的长度是相同的
比如说3和6的倒数的循环长度都是1
而怎么计算一个数的循环长度呢
只需要知道它能被多少长度的9整除就行了
3能被9整除,所以它的循环长度是1
7能被999999整除,商正好是循环体142857,所以它的循环长度是6
先求一个数的基数,如果是它本身,则计算它的循环长度
如果不是它自身,那它的循环长度等于基数的循环长度
我们规定1的循环长度是0,这样所以只含2,5为质因子的数的基数都为1,循环长度为0
import java.math.BigInteger;
public class Problem26
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.print("answer: ");
howmany();
long end = System.currentTimeMillis();
System.out.print("time: ");
System.out.println(end - start);
}
static void howmany()
{
int max = 0;
int n = 2;
for (int i = 2; i < 1000; i++)
{
int t = quchu(i);
if ( t == 1)
{
continue;
}
for (BigInteger j = BigInteger.valueOf(9); ;
j = j.multiply(BigInteger.TEN).add(BigInteger.valueOf(9)))
{
if ( j.remainder(BigInteger.valueOf(t)).compareTo(BigInteger.ZERO) == 0 )
{
int temp = (j.divide(BigInteger.valueOf(t)) + "").length();
if (temp > max)
{
n = i;
max = temp;
}
break;
}
}
// for (int j = 9; ; j = j * 10 + 9)
// {
// if (j % t == 0)
// {
// int temp = (j / t + "").length();
// if (temp > max)
// {
// max = temp;
// }
// break;
// }
// }
}
System.out.println(n);
}
static int quchu(int n)
{
while (true)
{
if (n % 2 == 0)
{
n = n / 2;
}
else if (n % 5 == 0)
{
n /= 5;
}
else
{
return n;
}
}
}
}
answer: 983
time: 284