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.

##运用了python的decimal模块取小数点精度,然后转换成str格式查找是否有重复的,从而计算出重复的长度。

#控制在1S内~


import decimal

decimal.getcontext().prec = 100

import time
start = time.time()
d = 7
l = 6
for i in range(1,1000):
s = str(decimal.Decimal(1)/decimal.Decimal(i))
for j in range(len(s)):
temp = s[j+3:].find(s[j:j+3])
if temp > 0 and 3+temp > l:
d = i
l = 3 + temp
# print(d,l)
decimal.getcontext().prec = 100 + l
break print(d,l)
print(time.time()-start)
>>>
983 982
0.6900279521942139
>>>

project euler 26:Reciprocal cycles的更多相关文章

  1. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

  2. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  3. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  4. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  5. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  6. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  7. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  8. Python练习题 043:Project Euler 015:方格路径

    本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...

  9. Python练习题 042:Project Euler 014:最长的考拉兹序列

    本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...

随机推荐

  1. DataTables在回调方法中使用api

    $(document).ready(function() { $('#example').dataTable( { "initComplete": function () { va ...

  2. easyui 实现Tooltip

    $('#btnAddr').tooltip({ content: $('<div class="table"></div>'), //弹出收件地址 show ...

  3. 用php理解指针--写给刚刚学习编程的人

    在刚刚学习编程时,可能for循环什么的还是可以理解,但是当学习到指针的时候,课上估计很多人就睡觉去了. 现在用两端php程序说明指针 先写一个简单的,大家都理解下 <?php class tex ...

  4. LeetCode_Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /* ...

  5. ET 与RETI 基于51单片机中断跳出指令“RETI”浅议

    最近在基于51单片机编程的过程中出现了个很奇怪的问题“程序执行中在寄存器EA=1,ET0=1,TR0=1条件下,单TF0=1时并没有执行中断”.在有过单片机中断编程经历者都知道当EA=1,ET0=1的 ...

  6. 【转】ubuntu下解压缩zip,tar,tar.gz和tar.bz2文件

    原文网址:http://blog.sina.com.cn/s/blog_5da93c8f0101h1uj.html 在Linux下面如何去压缩文件或者目录呢? 在这里我们将学习zip, tar, ta ...

  7. bzoj1753 [Usaco2005 qua]Who's in the Middle

    Description FJ is surveying his herd to find the most average cow. He wants to know how much milk th ...

  8. 【czy系列赛】czy的后宫4 && bzoj1925 [Sdoi2010]地精部落

    [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到-N之间的整数.他 ...

  9. SQL Server (MSSQLSERVER) 服务因 找不到指定的模块。 服务特定错误而停止。

    新装了sql server 2008,发现sqlserver 服务没法起来.查看系统日志是7024如下: 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 ...

  10. uva1220--树的最大独立集+判重

    题意是挑选尽量多的人,并且每个人都不和他的父节点同时出现,很明显的最大独立集问题,难点在于如何判断方案是否唯一. 详情请见刘汝佳<算法竞赛入门经典--第二版>P282 #include&l ...