Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the next highest number that can be factored into prime numbers {2,3,5,7}

Tags:

c++

modulus

I would like to write a function that is given an unsigned integer as an input argument and returns the next highest number that can be factored into prime numbers {2, 3, 5, 7}. Here is a short code snippet showing what I want to do.

unsigned int getHigherNumber(unsigned int x)
{
    // obtain the next highest number that can be factored into
    // prime numbers (2, 3, 5, and 7)
    if (~(x % 2 || x % 3 || x % 5 || x % 7))
        return  x;
    else
    {
         // ???
    }  
} // end

The purpose of this function is to find the number of zeros that an array should be padded with to ensure that algorithms such as FFTW (link) can run in an efficient manner. The given link discusses that the algorithm is optimal for an input of a length that can be factored into small primes.

As suggested in the comments to this question, if the FFTW algorithm is to be optimal, it appears that only numbers of the form 2^a * 3^b * 5^c * 7^d are allowed.

like image 753
Nicholas Kinar Avatar asked Nov 17 '25 12:11

Nicholas Kinar


2 Answers

For example, the standard FFTW distribution works most efficiently for arrays whose size can be factored into small primes (2, 3, 5, and 7), and otherwise it uses a slower general-purpose routine.

Really you dont need the next highest, but just something that is reasonably close. The simplest way to do this is just pick the power of 2 that is closest. This will waste at most the size of the original array.

To do this find the most significant bit and multiply it by 2.

The obvious way of finding the most significant bit:


    unsigned int v; // 32-bit word to find the log base 2 of  
    unsigned int r = 0; // r will be most significant bit

    while (v >>= 1)   
    {  
        r++;
    }  
like image 108
cacba Avatar answered Nov 20 '25 02:11

cacba


Building on a previous answer, If the array is going to be very big ~ 2^10, and you want to use as little extra space as possible while keeping it simple, you could also choose the largest power of 7 less than x, multiplied by the largest power of 5, and so on. That is

unsigned int getHigherNumber(unsigned int x)
{
    unsigned int ans=1;
    unsigned int primes[4]={2,3,5,7};
    for(int i=3; i>=0; i--)
    {
        for(int j=0; ; j++)
        {
            if(pow(primes[i],j)>x)
            {
                ans*=pow(7,j-1);
                if(i==0)
                    ans*=2;
                break;
            }
        }
    }
    return ans;
}

(Untested). I think that ought to get you a smaller number than just the nearest power of 2, at the cost of some speed. It definitely isn't optimal though.

like image 35
WanderingMathematician Avatar answered Nov 20 '25 01:11

WanderingMathematician



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!