TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(34)要发就发

从数学上分析:
假设第一行中的素数为n[1],n[2],n[3],...,n,...第二行中的差值为m[1],m[2],m[3],...,m[j],...,
其中m[j]=n[j+1]-n[j]. 则第二行连续N个数的和为:

- 程序:
- public class Test34{
- public static void main(String args[]){
- int j=0,count=0;
- int number[];
- number=new int[1000];
- System.out.printf("There are following primes sequences in firsr row:
- ");
- for(int i=3;i<=1993;i+=2)
- if(isSuShu(i)) number[j++]=i;//求出不超过1993的全部素数
- for(j=number.length-1;j>=0;j--){
- for(int i=0;i< number.length;i++){
- if(number[j]-number[i]==1898)
- System.out.printf("(%d). %3d,......%d
- ",++count,number[i],number[j]);
- }
- }
-
- }
- /**
- * 是素数
- */
- public static boolean isSuShu(int n) {
- boolean isSuShu = true;
- if (n == 1 || n == 2)
- return true;
- for (int i = 2; i < Math.sqrt(n) + 1; i += 1) {
- if (n % i == 0) {
- return false;
- }
- }
- if (isSuShu == true)
- return true;
- else
- return false;
- }
- }
- 运行:
复制代码
C:java>java Test34
There are following primes sequences in firsr row:(第一行中的素数序列)
(1). 89,......1987
(2). 53,......1951
(3). 3,......1901 C:java>
源码下载:http://file.javaxxz.com/2014/11/16/000932312.zip |
|