1
1
1ncomparable2017-09-10 00:06:33
C++ / C#
1ncomparable, 2017-09-10 00:06:33

Calculation of Fermat numbers. Why so slow?

Hello freshly baked friends sitting in the toaster.
I have such a problem: I decided for the sake of fun to "search" the Fermat numbers. Used System.Numerics.BigInteger. The methods of this structure had Pow, but it could raise to the power of Int32, so I built such a crutch:

static BigInteger PPow(byte num, BigInteger exp)
        {
            BigInteger result = 1;
            uint n = (uint)(exp / Int32.MaxValue);
            Parallel.For(0, n, i =>
            {
                lock (lockobj)
                {
                    result *= BigInteger.Pow(num, Int32.MaxValue);
                    exp -= Int32.MaxValue;
                }

            });
            result *= BigInteger.Pow(num, (int)exp);
            return result;
        }

The question is: Is there any way to speed up exponentiation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2017-09-10
@Sumor

Use Al-Koshi's Fast Exponentiation Algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question