Task :

Given a List [] of n integers , find minimum mumber to be inserted in a list, so that sum of all elements of list should equal the closest prime number .


Notes

  • List size is at least 2 .

  • List's numbers will only positives (n > 0) .

  • Repeatition of numbers in the list could occur .

  • The newer list's sum should equal the closest prime number .


Input >> Output Examples

1- minimumNumber ({3,1,2}) ==> return (1)

Explanation:

  • Since , the sum of the list's elements equal to (6) , the minimum number to be inserted to transform the sum to prime number is (1) , which will make the sum of the List equal the closest prime number (7) .

2-  minimumNumber ({2,12,8,4,6}) ==> return (5)

Explanation:

  • Since , the sum of the list's elements equal to (32) , the minimum number to be inserted to transform the sum to prime number is (5) , which will make the sum of the List equal the closest prime number (37) .

3- minimumNumber ({50,39,49,6,17,28}) ==> return (2)

Explanation:

  • Since , the sum of the list's elements equal to (189) , the minimum number to be inserted to transform the sum to prime number is (2) , which will make the sum of the List equal the closest prime number (191) .
public class Solution
{
public static int minimumNumber(int[] numbers)
{
//首先求得现在list中的数值之和
int sumOri = 0; for(int i=0;i<numbers.length;i++){
sumOri += numbers[i];
}
//用另一个数对原始和进行递增
int sumNow = sumOri;
boolean searchPrime = true;
//do...while递增原始数值,并判定是否为素数
while(searchPrime)
{ int count = 0;
for(int i=2;i<=Math.floor(sumNow/2);i++){
if(sumNow%i==0){
//合数进入
count++;
}
} //count为0,这个数是质数,停止查找
if(count == 0){
searchPrime = false;
}else{
//递增新的和
sumNow++;
} }
return sumNow-sumOri; // Do your magic!
}
}

寻找List之和的最近素数的更多相关文章

  1. 基于visual Studio2013解决C语言竞赛题之1085相邻之和素数

        题目 解决代码及点评 /************************************************************************/ /* ...

  2. HDU 1016 Prime Ring Problem (素数筛+DFS)

    题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...

  3. HDU 4548 美素数(打表)

    HDU  4548  美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...

  4. 基于visual Studio2013解决C语言竞赛题之0501挑选素数

        题目

  5. 数学#素数筛法 HDU 4548&POJ 2689

    找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...

  6. 【DFS】素数环问题

    题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...

  7. java高效判断素数

    java高效判断素数 package solution; public class Prime { // 偶数可以由有两个素数相加得到, 一个偶数可能有多个这样的两个素数, 请寻找到 这样两个素数,让 ...

  8. 【算法】C语言趣味程序设计编程百例精解

    C语言趣味程序设计编程百例精解 C/C++语言经典.实用.趣味程序设计编程百例精解(1)  https://wenku.baidu.com/view/b9f683c08bd63186bcebbc3c. ...

  9. 网络流 I - Fox And Dinner CodeForces - 510E

    Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...

随机推荐

  1. 【黑金ZYNQ7000系列原创视频教程】05.FPGA和ARM的初次结合——LED实验

    黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36639&extra=page%3D1 爱奇艺地址: http: ...

  2. 1.Math函数对象

    // 属性 Math.E //自然对数的底数(2.718281828459045) Math.PI //圆周率(3.141592653589793) Math.LN2 //2的自然对数(0.69314 ...

  3. Egret5.2.2 微信小游戏行的示例排行榜

    Egret5.2.2版本发布微信小游戏后,在开放数据域有一个默认排行榜.这个文件夹代码+图大小就22kb. 排行榜的效果就是示范用的,很丑...带翻页. 代码如下,基本就是使用canvas渲染了一个排 ...

  4. Python老王视频习题答案

    基础篇2:一切变量都是数据对象的引用sys.getrefcount('test') 查看引用计数变量命名不能以数字开头编码:ascii.unicode.utf-81.阅读str对象的help文档,并解 ...

  5. map容器结构体离散化

    小数坐标离散化: #include"string.h" #include"stdio.h" #include"iostream" #incl ...

  6. mysql 远程连接超时解决办法

    设置mysql远程连接root权限 在远程连接mysql的时候应该都碰到过,root用户无法远程连接mysql,只可以本地连,对外拒绝连接. 需要建立一个允许远程登录的数据库帐户,这样才可以进行在远程 ...

  7. 转!!git如何撤销上一次commit(或已push)

    原博文地址 : https://www.cnblogs.com/lyy-2016/p/6509707.html git如何撤销上一次commit操作 1.第一种情况:还没有push,只是在本地comm ...

  8. the source attachment does not contain the source for the file xxx.class无法关联到某个类

    问题描述: 按下列操作添加相应路径(这里是错误操作) 该问题仍旧无法解决: 注意:这里spring-webmvc-4.1.7.RELEASE.JAR中确实包含AnntationMethodHandle ...

  9. Day20 javaWeb监听器和国际化

    day20 JavaWeb监听器 三大组件: Servlet Listener Filter   Listener:监听器 初次相见:AWT 二次相见:SAX   监听器: 它是一个接口,内容由我们来 ...

  10. 产品开发过程描述xmind