Euler proved in one of his classic theorems that prime numbers are infinite in number. But can everynumber be expressed as a summation of four positive primes? I don’t know the answer. May be youcan help!!! I want your solution to be very efficient as I have a 386 machine at home. But the time limitspecified above is for a Pentium III 800 machine. The definition of prime number for this problem is“A prime number is a positive number which has exactly two distinct integer factors”. As for example37 is prime as it has exactly two distinct integer factors 37 and 1.

Input

The input contains one integer number N (N ≤ 10000000) in every line. This is the number you willhave to express as a summation of four primes. Input is terminated by end of file.

Output

For each line of input there is one line of output, which contains four prime numbers according tothe given condition. If the number cannot be expressed as a summation of four prime numbers printthe line ‘Impossible.’ in a single line. There can be multiple solutions. Any good solution will beaccepted.

Sample Input

24

36

46

Sample Output

3 11 3 7

3 7 13 13

11 11 17 7

问题链接:UVA10168 Summation of Four Primes

问题简述:(略)

问题分析

  这个问题是哥德巴赫猜想的一个扩展。

  筛选法是必要的,先找出素数。

  对于n < 8,则n不能表示为4个素数之和;

  若n为偶数,则两个素数为2和2,剩下部分的n(n-4)再分解;

  若n为奇数,则两个素数为2和3,剩下部分的n(n-5)再分解。

程序说明:(略)

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* UVA10168 Summation of Four Primes */#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>using namespace std;const int N = 1e7;
const int SQRTN = ceil(sqrt((double) N));
bool isPrime[N + 1];// Eratosthenes筛选法
void esieve(void)
{memset(isPrime, true, sizeof(isPrime));isPrime[0] = isPrime[1] = false;for(int i=2; i<=SQRTN; i++) {if(isPrime[i]) {for(int j=i*i; j<=N; j+=i)  //筛选isPrime[j] = false;}}
}int main()
{esieve();int n;while(~scanf("%d", &n)) {if(n < 8)printf("Impossible.\n");else if(n % 2 == 1) {/* 奇数 */for(int i=2; i<n-5; i++)if(isPrime[i] && isPrime[n - 5 - i]) {printf("2 3 %d %d\n", i, n - 5 - i);break;}} else {/* 偶数 */for(int i=2; i<n-4; i++)if(isPrime[i] && isPrime[n - 4 - i]) {printf("2 2 %d %d\n", i, n - 4 - i);break;}}}return 0;
}

UVA10168 Summation of Four Primes【筛选法】相关推荐

  1. python使用集合实现筛选法求素数-python素数筛选法浅析

    原理: 素数,指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数.在加密应用中起重要的位置,比如广为人知的RSA算法中,就是基于大整数的因式分解难题,寻找两个超大的素数然后相乘作 ...

  2. python筛选法求素数讲解_埃氏筛选法求素数 Python

    代码如下 def _odd_iter(): # 构建奇数序列 从3开始 n = 1 while True: n = n + 2 yield n def _not_divisible(n): retur ...

  3. 素数筛选法(埃氏筛 欧拉筛)

    质数筛选法 文章目录 质数筛选法 前言 一.埃氏筛 O(nloglogn)O(nloglogn)O(nloglogn) 二.欧拉筛O(n)O(n)O(n) 总结 前言 当需要大范围内的素数时,例如1e ...

  4. UVA160 Factors and Factorials【筛选法】

    The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. ...

  5. UVA583 UVALive5406 Prime Factors【素数因子+筛选法】

    Webster defines prime as: prime (pr¯im) n. [ME, fr. MF, fem. of prin first, L primus; akin to L prio ...

  6. 埃拉托色尼筛选法(Eratosthenes Sieve)分析

    最近看<java核心技术>看到集合章节,在最后位集(BitSet)部分给出了一个示例程序,使用了埃拉托色尼筛选法(Eratosthenes Sieve)求自然数2~n范围的所有素数 代码如 ...

  7. python filter求素数-埃拉托色尼筛选法

    质数:只可以被两个数整除,1和该数本身: 埃拉托色尼筛选法说明; (1)先把1删除(现今数学界1既不是质数也不是合数) (2)读取队列中当前最小的数2,然后把2的倍数删去 (3)读取队列中当前最小的数 ...

  8. Python:实现prime sieve eratosthenes埃拉托斯特尼素数筛选法算法(附完整源码)

    Python:实现prime sieve eratosthenes埃拉托斯特尼素数筛选法算法 # flake8: noqa def prime_sieve_eratosthenes(num):prim ...

  9. Java实现埃拉托色尼筛选法

    1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...

最新文章

  1. Java基础super关键字、final关键字、static关键字、匿名对象整理
  2. UGUI 帧动画插件
  3. python获取列表的最后一个元素
  4. 在网站建设过程中主要在哪几个方面为后期的网站优打好根基?
  5. php cgi路径解析,php.ini中的cgi.fix_pathinfo选项
  6. NLog日志框架使用探究
  7. 紧跟月影大佬的步伐,一起来学习如何写好JS(下)
  8. React生命周期(新)
  9. 性能优化篇(5):css 内部 or 外部,该如何选择?
  10. CV之Harris特征点检测器-兴趣点检测(详解)
  11. 金融行业的BI应用分析
  12. echarts-X轴不从0刻度开始 代码实现
  13. Python天天学_03_基础三
  14. linux 零拷贝golden,GoldenGate碎碎念
  15. 网易云音乐播放器缓存文件转换研究
  16. IOS:游戏存档修改,替换
  17. TIA博途中如何为IO设备分配设备名称?
  18. w10计算机用户名密码忘了,一招简单帮你解决win10系统电脑忘记开机密码
  19. centos7安装源设置基础软件仓库时出错
  20. LabelMe标注的.json文件批量转Dataset,通过查询到多种代码无法在我电脑上正常工作,发现他们有共同的报错,于是修改后能正常工作。

热门文章

  1. ArcGIS制图技巧系列(2)地形渲染
  2. 浅谈Flutter UI布局
  3. AppStore编年史 iOS游戏开发者的变迁路
  4. VC文件读写操作总结
  5. zabbix监控mysql内存_zabbix通过自定义脚本监控nginx,php-fpm和mysql占用内存数和进程的个数...
  6. reentrantlock原理_分享:synchronized和ReentrantLock的实现原理知识点
  7. r 语言 ggplot上添加平均值_技术贴 | R语言:ggplot堆叠图、冲积图、分组分面、面积图...
  8. mds算法 java_对OAF开发中的MDS的初步研究(转)
  9. Android 裁切踩坑
  10. Linux简单基本命令