Skip to content

Instantly share code, notes, and snippets.

@rpicard
Created April 27, 2014 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpicard/11336548 to your computer and use it in GitHub Desktop.
Save rpicard/11336548 to your computer and use it in GitHub Desktop.
Finally started to get some intuitive understanding of logarithms so I put it into code
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int base = atoi(argv[1]);
int value = atoi(argv[2]);
int i = 0;
# keep dividing value by base to reverse the exponent processes
while(value != 1) {
# doesn't handle fractions
if (value % base) {
return -1;
}
# count how many divides it takes to reverse the process
value = value / base;
i++;
}
printf("%d\n", i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment