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
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
单位分数在分子中包含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位循环周期。
找到d <1000 的值,其中1 / d包含其小数部分中最长的循环周期。
#include <stdio.h>
int get_len(int d){
int first_ind[1000] = {0};//记录每一种余数第一次出现的位置;
int ind = 0, y = 1;
do{
first_ind[y] = (ind++);//余数y第一次出现的位置为ind++;
y = (y * 10) % d;//1/7 余数为 1 * 10 % 7 = 3;
}while(y && first_ind[y] == 0);//余数不为0 切 第一次出现y的位置为0;
if(y == 0) return 0;//余数为0,不满足条件;
return ind - first_ind[y];//循环数的长度;
}
int main(){
int max = 0,ans = 0;
for(int i = 2; i < 1000; i++){
if(get_len(i) > max){
max = get_len(i);
ans = i;
}
}
printf("%d\n", ans);
return 0;
}