All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.greatbit.utils.math.Prime Maven / Gradle / Ivy

package ru.greatbit.utils.math;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by azee on 5/16/14.
 */
public class Prime {


    /**
     * Get list of simple primes
     * @param depth - depth of primes starting from 2
     * @return
     */
    public static List getPrimes(int depth){
        return getPrimes(2, depth);
    }

    /**
     * Get list of simple primes in a range
     * @param from - Lower value (inclusive)
     * @param to - Upper value (inclusive)
     * @return - list if Long values
     */
    public static List getPrimes(int from, int to){
        List result = new LinkedList();
        if (from < 2){
            from = 2;
        }
        for (int i = from; i <= to; i++){
            if (isPrime(i)){
                result.add(new Long(i));
            }
        }
        return result;
    }


    /**
     * Find out if the number is prime
     * @param value - value to detect
     * @return - true if prime, false - if not
     */
    public static boolean isPrime(long value){
        if (value < 2){
            return false;
        }
        for(int i = 2; i < value; i++){
            if(value % i == 0){
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy