Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define MAX_DIG 128 //maximum digits in any base
- #define MAX_BASES 16 //maximum number of bases stored
- typedef unsigned long long uint;
- typedef long long sint;
- typedef struct {
- uint N; //number of digits
- uint d[MAX_DIG]; //digits of a number
- uint r; //remainder, d mod(base-1)
- } numb;
- void print_numb(numb* n)
- {
- //print backwards
- if (n->N==0) {
- printf("0");
- } else {
- for(sint i=n->N-1; i>=0; i--) printf("%u_",n->d[i]);
- }
- }
- int main(void)
- {
- //todo:malloc for n (Nb) and for d (Nd), maybe not needed
- numb n[MAX_BASES]; //number table for all bases
- const uint x=0xe27efc30f694;
- uint xtmp=x;
- uint Nb=0;
- uint N=0; //number of calculated bases
- while(xtmp!=1) { //get msb bit position
- xtmp>>=1;
- Nb++;
- }
- printf("x=%u,Nb=%u:\r\n",x,Nb);
- //base is index of n
- //x mod(base-1) is in n['i'].r
- for(uint base=2; base<Nb+1; base<<=1) { //base2^N
- uint r=0;
- uint cd=0;
- uint pos_i=log2(base);
- xtmp=x;
- printf("\r\nbase=%u:\r\n",base);
- for(int pos=0; pos<Nb; pos+=pos_i) {
- uint d=xtmp&(base-1); //get digit in base
- xtmp>>=pos_i;
- printf("N=%u,cd=%u,d=%u\r\n",N,cd,d);
- n[N].d[cd]=d;
- r+=d; //sum of digits is remainder x mod(base-1)
- if(r>=base) r-=base-1; //modulo sum
- cd++;
- }
- //note
- if (r==base-1) r=0; //0 equivalent to base-1 when doing mod(base-1)
- //
- n[N].N=cd;
- n[N].r=r;
- N++;
- }
- //todo: display column as base, row as digits
- printf("\r\n\Displaying all values:\r\n");
- printf("x=%u,N=%u,Nb=%u:\r\n\r\n",x,N,Nb);
- for(int i=0; i<N;i++) {
- uint base=1<<(i+1);
- printf("base%u:",base);
- print_numb(n+i);
- printf(",r=%u",n[i].r);
- if(n[i].r==x%(base-1)) printf(" ...OK\r\n");
- else printf(" ...FAILED, r=%u\r\n",x%(base-1));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement