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. mysql在关闭时的几个阶段

    mysql关闭的大致过程 1.The shutdown process is initiated 初始化关机过程有许多种方法1.mysqladmin shutdown ; 2.kill pid_of_ ...

  2. MySQL用户远程登陆

    默认情况下,root用户是不具备远程登录的权限. 1.切换mysql表,查看当前用户信息 select host,user from user where user = 'root' 2.给用户进行授 ...

  3. 《Programming WPF》翻译 第9章 2.选择一个基类

    原文:<Programming WPF>翻译 第9章 2.选择一个基类 WPF提供了很多类,当创建一个自定义元素时,你可以从这些类中派生.图9-1显示了一组可能作为类--可能是合适的基类, ...

  4. PowerShell远程连接主机进行会话

    Get-ExecutionPolicy #脚本的执行策略set-ExecutionPolicy 枚举值 不同的策略,执行脚本的权限不同 允许开启远程 Enable-PSRemoting 添加域账户或者 ...

  5. 快速入门:触摸输入(使用 C#/VB/C++ 和 XAML 的 Windows 应用商店应用)

    原文 http://technet.microsoft.com/zh-cn/subscriptions/hh465387 快速入门:触摸输入(使用 C#/VB/C++ 和 XAML 的 Windows ...

  6. Android导航菜单横向左右滑动并和下方的控件实现联动

    这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢. 实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控 ...

  7. yii第一个应用blog

    1. 连接到数据库 大多数 Web 应用由数据库驱动,我们的测试应用也不例外.要使用数据库,我们首先需要告诉应用如何连接它.修改应用的配置文件 WebRoot/testdrive/protected/ ...

  8. DELL配置信息

    CPU详情CPU厂商 英特尔CPU (英特尔)Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHzCPU核心数 2CPU默认频率 2400 MhzCPU外频 533 MHz ...

  9. 【转】git与github在ubuntu下的使用 -- 不错

    原文网址:http://www.cnblogs.com/cocowool/archive/2010/10/19/1855616.html 最近开始使用git对kohana3的文档做一些补充的工作,使用 ...

  10. UML建模工具-火龙果软件

     官网地址:http://code.uml.com.cn/index.asp     Bridge桥梁模式    (待逆向) 桥梁模式,通过增加一个类,将抽象部分与它的实现部分分离,使它们都可以独立 ...