Gets the next twin prime that is near a power of 2 and greater than or equal to the given value - Java java.lang

Java examples for java.lang:Math Algorithm

Description

Gets the next twin prime that is near a power of 2 and greater than or equal to the given value

Demo Code


//package com.java2s;
import java.util.Arrays;

public class Main {
    /**//from  ww  w  .j ava  2s .c  o m
     * This array lits twin primes that are just larger than a power of 2. The 
     * prime in the list will be the larger of the twins, so the smaller can be 
     * obtained by subtracting 2 from the value stored. The list is stored in 
     * sorted order.<br>
     * Note, the last value stored is just under 2<sup>31</sup>, where the other 
     * values are just over 2<sup>x</sup> for x &lt; 31
     * 
     */
    public static final int[] twinPrimesP2 = { 7, //2^2 , twin with 5
            13, //2^3 , twin with 11
            19, //2^4 , twin with 17
            43, //2^5 , twin with 41
            73, //2^6 , twin with 71
            139, //2^7 , twin with 137
            271, //2^8 , twin with 269
            523, //2^9 , twin with 632
            1033, //2^10 , twin with 1031
            2083, //2^11 , twin with 2081
            4129, //2^12 , twin with 4127
            8221, //2^13 , twin with 8219
            16453, //2^14 , twin with 16451
            32803, //2^15 , twin with 32801
            65539, //2^16 , twin with 65537
            131113, //2^17 , twin with 131111
            262153, //2^18 , twin with 262151
            524353, //2^19 , twin with 524351
            1048891, //2^20 , twin with 1048889
            2097259, //2^21 , twin with 2097257
            4194583, //2^22 , twin with 4194581
            8388619, //2^23 , twin with 8388617
            16777291, //2^24 , twin with 16777289
            33554503, //2^25 , twin with 33554501
            67109323, //2^26 , twin with 67109321
            134217781, //2^27 , twin with 134217779
            268435579, //2^28 , twin with 268435577
            536871019, //2^29 , twin with 536871017
            1073741833, //2^30 , twin with 1073741831
            2147482951, //first twin under 2^31, twin with 2147482949
    };

    /**
     * Gets the next twin prime that is near a power of 2 and greater than or 
     * equal to the given value
     * 
     * @param m the integer to get a twine prime larger than
     * @return the a twin prime greater than or equal to 
     */
    public static int getNextPow2TwinPrime(int m) {
        int pos = Arrays.binarySearch(twinPrimesP2, m + 1);
        if (pos > 0)
            return twinPrimesP2[pos];
        else
            return twinPrimesP2[-pos - 1];
    }
}

Related Tutorials