第四周编程作业1-素数和

81 篇文章 0 订阅
23 篇文章 0 订阅
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;"><strong>题目内容:</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">现在,给定两个整数n和m,0<n<=m<=200,你的程序要计算第n个素数到第m个素数之间所有的素数的和,包括第n个素数和第m个素数。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;"><strong>输入格式:</strong>
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">两个整数,第一个表示n,第二个表示m。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;"><strong>输出格式:</strong>
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">一个整数,表示第n个素数到第m个素数之间所有的素数的和,包括第n个素数和第m个素数。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;"><strong>输入样例:</strong>
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">2 4</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;"><strong>输出样例:</strong>
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; outline: none; color: rgb(102, 102, 102); font-family: Arial, 'Hiragino Sans GB', SimSun, 宋体, Georgia, serif; line-height: 22px; white-space: pre-wrap;">15</p>
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m= in.nextInt();
		int i,j,sum=0,count=0;
		boolean tag=true;
		double num;
		
		for(i=2;count<m;i++){
			num = Math.sqrt((double) i);
			tag=true;
			for(j =2;j<=num;j++)//判断是否是素数,tag为true时,为素数
				if(i%j==0){
						tag=false;break;
				}
			if(tag){
				count++;
				if(count>=n)//如果是素数且在n到m之间,求和
					sum+=i;
			}
		}
		System.out.println(sum);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值