Сегодня хотелось бы рассмотреть два алгоритма на java. Первая программа ищет простые числа методом Решета Эратосфена. Код можно посмотреть тут:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package simplenumber; import java.util.ArrayList; import java.util.List; /** * * @author javamain */ public class SimpleNumber { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int n = Math.abs( 100 ); List<Boolean> sieve=new ArrayList<Boolean>(); for( int i = 0; i <= n; i++ ) sieve.add(Boolean.TRUE); for( int i = 2; i <= n; i++ ) for( int k = i + 1; k <= n; k++ ) if( k % i == 0 ) sieve.set(k, Boolean.FALSE); for(int i=2 ; i<n ; i++) { if( sieve.get( i ) == true) System.out.println( (new Integer(i)).toString()+"\n"); } } }
Второй алгоритм ищет два рендомных числа больше 5000000 до 10000000.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pkg2bignumber; import java.util.Random; /** * * @author javamain */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Random rand=new Random(); double number,number2; for(int i=0; i<5000000; i++){ number = Math.round(rand.nextDouble()*5000000)+5000000; number2 = Math.round(rand.nextDouble()*5000000)+5000000; System.out.println(((Double)number).toString()+" "+((Double)number).toString()+"\n"); } } }
ссылка на оригинал статьи http://habrahabr.ru/post/270771/
Добавить комментарий