EularProject 41:最长的n位Pandigital素数问题
2015-07-30 15:37
323 查看
Pandigital prime
Problem 41
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
Answer:
7652413
Completed on Thu, 30 Jul 2015, 08:32
Go to the thread for problem 41 in the forum.
首先发现3k和3k-1位的ppandigital肯定能被3整除,所以就剩下1,4,7,10,13。。。猜测题意应该是9以内的所以就试试7咯,代码是前面代码扩展,就出结果了
/article/1366109.html
/article/1322119.html
Problem 41
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
Answer:
7652413
Completed on Thu, 30 Jul 2015, 08:32
Go to the thread for problem 41 in the forum.
首先发现3k和3k-1位的ppandigital肯定能被3整除,所以就剩下1,4,7,10,13。。。猜测题意应该是9以内的所以就试试7咯,代码是前面代码扩展,就出结果了
/article/1366109.html
/article/1322119.html
__author__ = 'zhengyi' import math from functools import reduce def IsPrime(x): if x==1: return False k=int(math.sqrt(x))+1 for i in range(2,k): if x%i==0: return False return True def reducefunc(x,y): return 10*x+y def func1(x): i=1 val=1 while val<x: i+=1 val*=i if val==x: return (1,i,0) else: step=val//i k=x//step return (k,i-1,x%(k*step)) def func2(x): lst=[] result=func1(x) while result[2]!=0: lst.append(result[0:2]) result=func1(result[2]) lst.append(result[0:2]) return lst def func3(x,clst): result=[] count=len(clst) lst=func2(x) length=len(lst) for i in range(0,length): if i<length-1: delta=lst[i][0] position=lst[i][1]+1 while count>position: result.append(clst[-count]) del clst[-count] count-=1 result.append(clst[-position+delta]) del clst[-position+delta] count-=1 else: delta=lst[i][0]-1 position=lst[i][1]+1 while count>position: result.append(clst[-count]) del clst[-count] count-=1 result.append(clst[-position+delta]) del clst[-position+delta] count-=1 while count>0: result.append(clst[-1]) del clst[-1] count-=1 return result charlist=[i for i in range(7,0,-1)] k=0 while True: k+=1 temp=func3(k,charlist.copy()) data=reduce(reducefunc,temp) if IsPrime(data): print(data) break print("Get it")
相关文章推荐
- [MFC]选择目录对话框和选择文件对话框 [转]
- 多线程学习
- 编译librtmp for Android
- 进程线程的解释
- cocos2d-x 如何保持屏幕常亮
- Win10内测用户大福利:不必重装win10正式版
- GDI有用的函数 --- 收集
- Spans进阶
- monkey、monkeyrunner使用方法
- Cocos2d-x TitleMap A*算法实现。(初版)
- Java 正则表达式详解
- 滚动列表时隐藏输入框
- Zynq片内XADC应用笔记
- c语言实现几种排序算法OC实现
- server-u做的FTP服务器客户端访问文件乱码的解决办法
- C# Winform 跨线程更新UI控件常用方法总结(转)
- 64. Minimum Path Sum
- hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***
- 【C语言探索之旅】 第二部分第十课:练习题和习作
- iOS开发UI篇—UITableviewcell的性能优化和缓存机制