欧拉计划 第二十六题

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值