int main(int argc, const char *argv[]) { int num = 0; while (1) { if(scanf("%d", &num) == 0) { printf("please input right data\n\n"); return 1; } decimal_to_binary(num); decimal_to_hex(num); } return 0; }
// Algorithm: Modular Power: x^e(MOD n). long int ModPower(long int x, long int e, long int n) {// To calculate y:=x^e(MOD n). //long y; long int y; long int t; int i; int BitLength_e; char b[STACK_SIZE]; //printf("e(decimal) = %ld\n",e); decimal_to_binary(e,b); if(print_flag) printf("b = %s\n", b); BitLength_e = strlen(b); y = x; reverse_string(b); for(i = BitLength_e - 2; i >= 0 ; i--) { if(print_flag) printf("\nb[%d]=%c", i, b[i]); if(b[i] == '0') t = 1; else t = x; y = (y * y) MOD n; if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } y = (y*t) MOD n; if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } } if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } return y; } // end of ModPower().
void binary_system(int check_bi_zero_count, int binary_count) { int x,y; int arr[1000]; int result=0; for(x=1; x<=binary_count; x++) { arr[x-1]=decimal_to_binary(x); } for(y=0; y<binary_count; y++) { if(check_bi_zero_count==arr[y]) { result++; } } printf("%d", result); }
/* Algorithm: Modular Power: x^e(mod n) using the repeated square-and-multiply algorithm */ long int ModPower(long int x, long int e, long int n) { // To calculate y:=x^e(mod n). //long y; long int y; long int t; int i; int BitLength_e; char b[STACK_SIZE]; //printf("e(decimal) = %ld\n",e); decimal_to_binary(e,b); if(print_flag) printf("b = %s\n", b); BitLength_e = strlen(b); y = x; reverse_string(b); for(i = BitLength_e - 2; i >= 0 ; i--) { if(print_flag) printf("\nb[%d]=%c", i, b[i]); if(b[i] == '0') t = 1; else t = x; y = y * y; y = modulo (y, n); y = y*t; y = modulo (y, n); } return y; }