寻找List之和的最近素数

简介: 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 .

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!
    }
}

 

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
相关文章
|
9月前
|
人工智能 自然语言处理 机器人
一键部署开源DeepSeek并集成到钉钉
DeepSeek发布了两款先进AI模型V3和R1,分别适用于对话AI、内容生成及推理任务。由于官方API流量限制,阿里云推出了私有化部署方案,无需编写代码即可完成部署,并通过计算巢AppFlow集成到钉钉等渠道。用户可独享资源,避免服务不可用问题。部署步骤包括选择机器资源、配置安全组、创建应用与连接流,最终发布应用版本,实现稳定高效的AI服务。
712 4
一键部署开源DeepSeek并集成到钉钉
|
8月前
|
存储 安全 Linux
CentOS 7.9系统备份:每日定期发送最新备份文件到另一台服务器。
注意,这个解决方案忽略了很多细节,例如错误处理和通知、备份版本控制、循环处理旧的备份文件等等。此外,你也应该尽量保持源服务器和目标服务器之间快速,稳定且安全的网络连接,并且目标服务器应该有足够的空间用于存放每天的备份文件。如果你需要更高级的备份解决方案,可能需要考虑一下使用专门的备份工具或者服务。
355 18
|
8月前
|
开发框架 .NET API
.NET 10首个预览版发布:重大改进与新特性概览!
.NET 10首个预览版发布:重大改进与新特性概览!
268 3
.NET 10首个预览版发布:重大改进与新特性概览!
|
7月前
|
传感器 安全 机器人
《特斯拉Optimus Gen - 2:多模态感知如何重塑具身智能未来》
特斯拉推出的Optimus Gen-2,凭借多模态感知技术成为机器人具身智能发展的里程碑。它通过视觉、听觉和触觉等多种传感器协同工作,实现对环境的全面理解。视觉摄像头帮助其精准导航与避障,高精度麦克风使其理解语音指令,触觉传感器让操作更加细腻安全。这些能力使Optimus Gen-2能快速适应工厂、家庭等复杂场景,提升人机协作效率,并在医疗、教育等领域展现潜力。多模态感知技术不仅推动了机器人自主学习与决策能力的发展,还预示着未来机器人将更深入地融入人类社会,为生产与生活带来革命性变化。
345 0
|
JSON Java fastjson
简单实现_实体类与Json字符串互相转换
简单实现_实体类与Json字符串互相转换
374 1
|
存储 机器学习/深度学习 编解码
计算机视觉的基础概念与入门
之前学习了一下 Python 环境下计算机视觉方面的一些应用(主要是 OpenCV),但是对于计算机视觉方面的种种概念都是一笔带过,计算机视觉是一个很大的领域,在深入它之前 ,有必要对其中的一些基础概念有一个宏观的理解。
|
自然语言处理 Java 索引
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
492 0
|
网络安全 开发工具 云计算
服务器看代码阿里云
随着云计算技术的发展,阿里云作为国内领先的云计算服务提供商,其服务器受到广大用户青睐。本文主要介绍如何在阿里云服务器上便捷地查看与管理代码,如使用SSH连接服务器并通过命令行工具打开文件,以及利用Git进行版本控制和协作开发,提高代码管理效率。无论个人开发者还是企业团队,都能借助阿里云服务器高效地部署与管理应用程序,提升工作效率及产品质量。
212 10
|
小程序 定位技术
【微信小程序-原生开发+TDesign】地图导航(wx.openLocation的使用)
【微信小程序-原生开发+TDesign】地图导航(wx.openLocation的使用)
420 0
SpringBoot之内部配置加载顺序和外部配置加载顺序
SpringBoot之内部配置加载顺序和外部配置加载顺序