Скачиваний:
15
Добавлен:
01.05.2014
Размер:
1.93 Кб
Скачать
/*
* Primes.java
*
* Created on April 26, 2007, 12:20 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package diffiehellmann;
import java.math.BigInteger;
import java.lang.*;
import java.util.Random;
/**
*
* @author adm
*/
public class Primes {


private int start; // no default
public int count = 20; // default value

Primes(int sta) { // constructor with one arg
start = sta;
}

public boolean isPrime(int p) {
int i;
for(i=2;i<(p/2);i++) {
if ( i*(p/i) == p ) return(false);
}
return(true);
}

public void Generate(int arr[]) { // make primes
int sta = start;
int cnt = count;
int idx = 0;

while ( cnt > 0 ) {
if ( isPrime(sta) ) {
arr[idx++] = sta;
cnt--;
}
sta++;
}
}
}

class Mclass {
public static void main(String argv[]) {

Primes p;
int arrp[];
int len;
int sta = 2;
int cnt = 0; // dummy default
int i;

len = argv.length;
if ( len > 0 ) {
sta = Integer.parseInt(argv[0]);
if ( len > 1 ) {
cnt = Integer.parseInt(argv[1]);
}
}
p = new Primes(sta);
if ( len > 1 ) p.count = cnt; // set if user specifies a count


arrp = new int[p.count];
p.Generate(arrp);

//for(i=1;i<=p.count;i++)
System.out.println("Prime " + "" +" is " + arrp[arrp.length-1]);
BigInteger bi = new BigInteger(50, 10, new Random());

System.out.println("Prime " + "" +" is " + bi.intValue()+" is prime? : "+ (new Primes(1)).isPrime(bi.intValue()));
}
}

Соседние файлы в папке diffiehellmann