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. Sencha中Element的使用

    在sencha touch中如果你要是用模板来构造一些UI,那么你就必定要去操作Element,如下是我对Element的一些操作和遇到的问题 获取Elenent Ext.get("ID&q ...

  2. WEB安全第二篇--用文件搞定服务器:任意文件上传、文件包含与任意目录文件遍历

    零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...

  3. C++ 初始化函数的实现

    http://www.cppblog.com/xlshcn/archive/2007/11/21/37088.aspx

  4. [SQL] 常用查询脚本

    查询哪些存储过程使用了某个表 select b.name from syscomments a,sysobjects b where a.id=b.id and a.text LIKE '%ftblo ...

  5. PL/SQL常用设置

    tools-->preferences-->user interface-->editor-->AutoReplace AutoReplaceWhen enabled, you ...

  6. AWR之-enq TX - row lock contention的性能故障-转

    1 对这一个小时进行AWR的收集和分析,首先,从报告头中看到DB Time达到近500分钟,(DB Time)/Elapsed=8,这个比值偏高:   Snap Id Snap Time Sessio ...

  7. CentOS 6.7 配置LVM (逻辑卷管理)

    LVM 简介 LVM是逻辑盘卷组管理 (Logical Volume Manager) 的简称. LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性,在一定程度上解决普通磁盘分区带来 ...

  8. LINEAR HASH Partitioning

    MySQL :: MySQL 8.0 Reference Manual :: 23.2.4.1 LINEAR HASH Partitioning https://dev.mysql.com/doc/r ...

  9. 多线程下载图片,滑动tableView崩溃--资源抢夺问题

    最近练习使用NSoperation模拟SDWebImage下载图片,发生了崩溃的问题,还专门写博客记录这件事情: http://www.cnblogs.com/tufei7/p/7074030.htm ...

  10. shell export 命令

    export 命令作用是 把变量导出 也可以用export来定义环境变量 导入 定义的变量 这样的话类似于python面向对象的self.变量 一样 在脚本到处调用这个变量