Re: constexpr is really very smart!

Liste des GroupesRevenir à cl c++ 
Sujet : Re: constexpr is really very smart!
De : student (at) *nospam* invalid.invalid (Student Project)
Groupes : comp.lang.c++
Date : 19. Dec 2024, 20:14:19
Autres entêtes
Organisation : To protect and to server
Message-ID : <vk1rgl$3l55g$1@paganini.bofh.team>
References : 1 2 3 4 5
On 18/12/2024 11:51, Michael S wrote:
Iterative:
 
static long long fib(long n)
{
   if (n <= 0)
     return 0;
   // find MS bit
   unsigned long tmp = n;
   unsigned long bit = 1;
   while (tmp > 1) {
     bit += bit;
     tmp /= 2;
   }
   // for n > 0
   // fib(n*2)   = (fib(n-1)+fib(n+1))*fib(n)
   // fib(n*2+1) = fib(n)*fib(n)+fib(n+1)*fib(n+1)
   long long a = 0, b = 1;
   while (bit > 1) {
     long long c = a + b; // fib(n+1)
     bit /= 2;
     if ((n & bit)==0) { // (n-1,n) => (n*2-1,n*2)
       c += a;
       a = a*a + b*b;
       b = c*b;
     } else {            // (n-1,n) => (n*2,n*2+1)
       a = (a + c)*b;
       b = b*b + c*c;
     }
   }
   return b;
}

Here is my test result of this iterative function:

D:\CmdLine\C_Cpp\Chrono06>program
Testing fibonacci function
fib(92) = 7540113804746346429
Time taken by fib: 0 microseconds
 
D:\CmdLine\C_Cpp\Chrono06>program
Testing fibonacci function
fib(92) = 7540113804746346429
Time taken by fib: 0 microseconds
 
D:\CmdLine\C_Cpp\Chrono06>program
Testing fibonacci function
fib(92) = 7540113804746346429
Time taken by fib: 0 microseconds
 
D:\CmdLine\C_Cpp\Chrono06>program
Testing fibonacci function
fib(92) = 7540113804746346429
Time taken by fib: 0 microseconds

The full program to run in G++ is:

<+++++++++++++++++++++++++++++++++++++++++++++>

#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

long long fib(long n)
{
     if (n <= 0)
         return 0;
     // find MS bit
     unsigned long tmp = n;
     unsigned long bit = 1;
     while (tmp > 1)
     {
         bit += bit;
         tmp /= 2;
     }
     // for n > 0
     // fib(n*2)   = (fib(n-1)+fib(n+1))*fib(n)
     // fib(n*2+1) = fib(n)*fib(n)+fib(n+1)*fib(n+1)
     long long a = 0, b = 1;
     while (bit > 1)
     {
         long long c = a + b; // fib(n+1)
         bit /= 2;
         if ((n & bit) == 0)
         { // (n-1,n) => (n*2-1,n*2)
             c += a;
             a = a * a + b * b;
             b = c * b;
         }
         else
         { // (n-1,n) => (n*2,n*2+1)
             a = (a + c) * b;
             b = b * b + c * c;
         }
     }
     return b;
}

int main(void)
{
     cout << "Testing fibonacci function\n";
     auto start = high_resolution_clock::now();
     constexpr int num = 92;
     long long result = fib (num);
     auto stop = high_resolution_clock::now();

     auto duration = duration_cast<microseconds>(stop - start);
     cout << "fib(" << num << ") = " << result << "\n";
     cout << "Time taken by fib: " << duration.count() << " microseconds\n";

}

<+++++++++++++++++++++++++++++++++++++++++++++>



Date Sujet#  Auteur
15 Dec 24 * constexpr is really very smart!39Student Project
16 Dec 24 +* Re: constexpr is really very smart!35Michael S
16 Dec 24 i+* Re: constexpr is really very smart!8David Brown
16 Dec 24 ii`* Re: constexpr is really very smart!7Michael S
16 Dec 24 ii +* Re: constexpr is really very smart!3David Brown
17 Dec 24 ii i`* Re: constexpr is really very smart!2Michael S
17 Dec 24 ii i `- Re: constexpr is really very smart!1David Brown
18 Dec 24 ii `* Re: constexpr is really very smart!3Tim Rentsch
18 Dec 24 ii  `* Re: constexpr is really very smart!2Michael S
18 Dec 24 ii   `- Re: constexpr is really very smart!1Tim Rentsch
17 Dec 24 i`* Re: constexpr is really very smart!26Tim Rentsch
18 Dec 24 i `* Re: constexpr is really very smart!25Michael S
18 Dec 24 i  +* Re: constexpr is really very smart!13Michael S
19 Dec 24 i  i+* Re: constexpr is really very smart!11Tim Rentsch
20 Dec 24 i  ii`* Re: constexpr is really very smart!10Michael S
20 Dec 24 i  ii `* Re: constexpr is really very smart!9Tim Rentsch
21 Dec 24 i  ii  `* Re: constexpr is really very smart!8Michael S
22 Dec 24 i  ii   `* Re: constexpr is really very smart!7Tim Rentsch
23 Dec 24 i  ii    `* Re: constexpr is really very smart!6Michael S
24 Dec 24 i  ii     `* Re: constexpr is really very smart!5Tim Rentsch
25 Dec 24 i  ii      `* Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)4Michael S
25 Dec 24 i  ii       +* Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)2Michael S
27 Dec 24 i  ii       i`- Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)1Tim Rentsch
27 Dec 24 i  ii       `- Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)1Tim Rentsch
19 Dec 24 i  i`- Re: constexpr is really very smart!1Student Project
18 Dec 24 i  `* Re: constexpr is really very smart!11Tim Rentsch
18 Dec 24 i   `* Re: constexpr is really very smart!10Michael S
19 Dec 24 i    `* Re: constexpr is really very smart!9Tim Rentsch
19 Dec 24 i     `* Re: constexpr is really very smart!8Michael S
20 Dec 24 i      `* Re: constexpr is really very smart!7Tim Rentsch
21 Dec 24 i       `* Re: constexpr is really very smart!6Michael S
21 Dec 24 i        +* Re: constexpr is really very smart!2James Kuyper
22 Dec 24 i        i`- Re: constexpr is really very smart!1Michael S
22 Dec 24 i        `* Re: constexpr is really very smart!3Tim Rentsch
22 Dec 24 i         `* Re: constexpr is really very smart!2Michael S
23 Dec 24 i          `- Re: constexpr is really very smart!1Tim Rentsch
17 Dec 24 `* Re: constexpr is really very smart!3Keith Thompson
18 Dec 24  `* Re: constexpr is really very smart!2Student Project
18 Dec 24   `- Re: constexpr is really very smart!1Michael S

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal