题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的。例如,2143就是一个4位全数字数,同时它恰好也是一个素数。

最大的全数字的素数是多少?

思路:

  1. 最大全排列素数可以从 n = 9 使用 perv_permutation 倒序生成。
  2. 当 n = 9 或者 n = 8 时生成的全排列构成的数一定不是素数,因为它一定能被 3 整除,所以从 7 开始枚举。
  3. 因为生成的数字太大,所以采用米勒测试判断素数。

/*************************************************************************
> 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( 米勒测试 + 生成全排列 )的更多相关文章

  1. 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 ...

  2. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  3. Project Euler 50 Consecutive prime sum

    题意: 素数41可以写成六个连续素数的和: 41 = 2 + 3 + 5 + 7 + 11 + 13 在小于一百的素数中,41能够被写成最多的连续素数的和. 在小于一千的素数中,953能够被写成最多的 ...

  4. Project Euler 87 :Prime power triples 素数幂三元组

    Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...

  5. Project Euler 77:Prime summations

    原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...

  6. 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 ...

  7. Project Euler 38 Pandigital multiples

    题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...

  8. Project Euler 32 Pandigital products

    题意:找出所有形如 39 × 186 = 7254 这种,由 1 - 9,9个数字构成的等式的和,注意相同的积不计算两次 思路:如下面两种方法 方法一:暴力枚举间断点 /*************** ...

  9. Project Euler 27 Quadratic primes( 米勒测试 + 推导性质 )

    题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40( ...

随机推荐

  1. [jQuery]jQuery获取URL参数

    // jQuery url get parameters function [获取URL的GET参数值]// <code>// var GET = $.urlGet(); //获取URL的 ...

  2. HDU 2717

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 国内物联网平台初探(三) ——QQ物联·智能硬件开放平台

    平台定位 将QQ帐号体系.好友关系链.QQ消息通道及音视频服务等核心能力提供给可穿戴设备.智能家居.智能车载.传统硬件等领域的合作伙伴,实现用户与设备.设备与设备.设备与服务之间的联动. 实现用户与设 ...

  4. js滚动

    有选择性的重复造一些轮子,未必是件坏事.Aaron的博客上加了一个悬浮菜单,貌似显得很高大上了.虽然这类小把戏也不是头一次见了,但是从未自己写过.今天就选择性的拿这个功能写一写.下面是这个轮子的开发过 ...

  5. 一个php+jquery+json+ajax实例

    json.php <!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  6. hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串

    题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...

  7. 移动测试基础—adb、monkey命令

    最近打算把移动测试相关的知识总结一下,先从基础开始吧,总结一下adb.monkey命令 adb常用命令总结 adb / adb -help 使用帮助 adb devices 查看连接到电脑的设备 ad ...

  8. JavaScript数组和json的区别

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  9. JavaScript函数传参

    函数传参一: <html> <head> <meta charset="utf-8"> <title>无标题文档</title ...

  10. SPL类

    用途:对类,方法,属性,参数的提取生成文档:自动加载插件 实列化类同于new:$ref = new ReflectionClass($classname);$class = $ref->newI ...