Skip to content

Instantly share code, notes, and snippets.

@rriemann
Forked from anonymous/gist:729557
Created July 7, 2012 15:04
Show Gist options
  • Select an option

  • Save rriemann/3066809 to your computer and use it in GitHub Desktop.

Select an option

Save rriemann/3066809 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous revised this gist Dec 5, 2010. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,9 @@ U32 icbrt64(U64 x) {

    int main()
    {
    for (int i=1; i<=2642245; ++i) { // 2642245 = floor(cbrt(2^64))
    U32 max = 2642245; // floor(cbrt(2^64))

    for (U32 i=1; i<=max; ++i) { //
    U64 v = (U64) i*i*i;
    U64 cb = icbrt64(v);

    @@ -41,6 +43,12 @@ int main()
    break;
    }
    }


    // check max value
    U64 v = ~0ull;
    if (icbrt64(v) != max) {
    printf("err3!\n");
    }

    return 0;
    }
  2. @invalid-email-address Anonymous created this gist Dec 5, 2010.
    46 changes: 46 additions & 0 deletions gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #include <stdio.h>

    typedef unsigned int U32;
    typedef unsigned __int64 U64;

    // ---- actual cube root code

    U32 icbrt64(U64 x) {
    int s;
    U32 y;
    U64 b;

    y = 0;
    for (s = 63; s >= 0; s -= 3) {
    y += y;
    b = 3*y*((U64) y + 1) + 1;
    if ((x >> s) >= b) {
    x -= b << s;
    y++;
    }
    }
    return y;
    }

    // ---- test driver

    int main()
    {
    for (int i=1; i<=2642245; ++i) { // 2642245 = floor(cbrt(2^64))
    U64 v = (U64) i*i*i;
    U64 cb = icbrt64(v);

    if (cb != i) {
    printf("err1! i=%d cb=%d\n", i, (int) cb);
    break;
    }

    cb = icbrt64(v-1);
    if (cb != i-1) {
    printf("err2! i-1=%d cb=%d\n", i-1, cb);
    break;
    }
    }

    return 0;
    }