View Single Post
Old 2021-11-25, 19:03   #2
R. Gerbicz
 
R. Gerbicz's Avatar
 
"Robert Gerbicz"
Oct 2005
Hungary

112×13 Posts
Default

You need only mpz_setbit(), this is optimal in speed/complexity, (you set the ones in the expansion).

For example this runs in 0 second: (maybe you should build up from the other direction, because in my code the memory could be a little fragmented if you enlarge the number multiple times)
Code:
#include <stdio.h>
#include <time.h>
#include "gmp.h"

int main(){
    
    mpz_t n;
    
    mpz_init(n);
    
    time_t sec=time(NULL);
    for(int i=0;i<1000000;i++)
        mpz_setbit(n,i);
    
    gmp_printf("%Zd\n",n);
    printf("time=%ld sec.\n",time(NULL)-sec);

    mpz_clear(n);
    
    return 0;
}
R. Gerbicz is offline   Reply With Quote