Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )
题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的。例如,2143就是一个4位全数字数,同时它恰好也是一个素数。
最大的全数字的素数是多少?
思路:
- 最大全排列素数可以从 n = 9 使用 perv_permutation 倒序生成。
- 当 n = 9 或者 n = 8 时生成的全排列构成的数一定不是素数,因为它一定能被 3 整除,所以从 7 开始枚举。
- 因为生成的数字太大,所以采用米勒测试判断素数。
/*************************************************************************
> File Name: euler041.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年07月01日 星期六 14时46分33秒
************************************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <inttypes.h>
#define MAX_ROUND 30
int32_t quick_pow(int64_t a , int64_t b , int64_t mod) {
int64_t ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret % mod;
}
bool R_M_TEST(int64_t n) {
int64_t x;
for (int32_t i = 0 ; i < MAX_ROUND ; i++) {
x = (rand() % (n - 1)) + 1;
if (quick_pow(x , n - 1 , n) != 1) return false;
}
return true;
}
int32_t main() {
int64_t tmp;
int32_t num[9] = {0} , flag = 0;
srand(time(NULL));
for(int32_t i = 7 ; i >= 1 ; i--) {
for (int32_t j = 0 ; j < i ; j++)
num[j] = i - j;
do {
tmp = 0;
for (int32_t j = 0 ; j < i ; j++)
tmp = tmp * 10 + (int64_t)num[j];
if (R_M_TEST(tmp)) {
printf("ans = " "%"PRId64"\n",tmp);
flag = 1; break;
}
} while(std::prev_permutation(num , num + i));
if (flag) break;
}
return 0;
}
Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )的更多相关文章
- Project Euler:Problem 41 Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- Project Euler 50 Consecutive prime sum
题意: 素数41可以写成六个连续素数的和: 41 = 2 + 3 + 5 + 7 + 11 + 13 在小于一百的素数中,41能够被写成最多的连续素数的和. 在小于一千的素数中,953能够被写成最多的 ...
- Project Euler 87 :Prime power triples 素数幂三元组
Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- Project Euler Problem 7-10001st prime
素数线性筛 MAXN = 110100 prime = [0 for i in range(210000)] for i in range(2,MAXN): if prime[i] == 0: pri ...
- Project Euler 38 Pandigital multiples
题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...
- Project Euler 32 Pandigital products
题意:找出所有形如 39 × 186 = 7254 这种,由 1 - 9,9个数字构成的等式的和,注意相同的积不计算两次 思路:如下面两种方法 方法一:暴力枚举间断点 /*************** ...
- Project Euler 27 Quadratic primes( 米勒测试 + 推导性质 )
题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40( ...
随机推荐
- 0223实战:MySQL Sending data导致查询很慢的问题详细分析
转自博客http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据 ...
- Windows系统SNMP数据监测与OID
Windows系统SNMP数据监测与OID http://blog.csdn.net/liuhuango123/article/details/52253953
- NEFU 115
刚开始,做了水题 #include <iostream> #include <cstdio> #include <algorithm> using namespac ...
- ETL (数据仓库技术)
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL一词较常用在数 ...
- h5-8 canvas
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 国内物联网平台初探(六) ——庆科云FogCloud
平台定位 FogCloud 快速接入智能硬件 FogCloud为开发者提供便捷的智能硬件接入服务,真正实现敏捷开发,快速迭代. FogCloud提供功能强大的云端服务 包括 产品/APP管理 ,消息通 ...
- 分析AWR报告
1.AWR报告头信息 DB Name :数据库名字 DBid: 数据库id Elapsed:采样时间段 DB Time:用户操作花费的时间,不包括Oracle后台进程消耗的时间 DB Time远小于E ...
- java.sql.SQLException: Field 'id' doesn't have a default value解决方案
转自:https://blog.csdn.net/xinghuo0007/article/details/51810867 自增长:java.sql.SQLException: Field 'id' ...
- ROS单线多拨pppoe
#设置内网IP地址 / ip add add interface=ether1 address=192.168.0.254/255.255.255.0 #设置共享上网 / ip firewall na ...
- Class工具类
Class工具类,提供操作class类的方法,源码如下: import java.io.File; import java.io.FileFilter; import java.io.IOExcept ...