分子爲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