I'm just mucking around with C as a learner, and wrote this little function...
char *getPlaceSuffix(int number) {
static char *suffixes[] = {"st", "nd", "rd", "th"};
if (number >= 11 && number <= 13) {
return suffixes[3];
} else {
while (number > 10) {
number -= 10;
}
if (number >= 1 && number <= 3) {
return suffixes[number - 1];
} else {
return suffixes[3];
}
}
}
I tweeted the link, and Konrad Rudolph informed me my method of getting the least significant number was O(n) and not very efficient.
Unfortunately, it’s O(n) for very large number – to make it O(logn), adjust the while loop for higher powers of 10 …
I'm not too familiar with Big O notation, but I get the gist that O(n) isn't too efficient?
As you can see from my code sample, I deduct 10 until the number is one digit long, so I can compare it to see which suffix is appropriate. I had a quick play with division and modulus but couldn't figure it out.
So, my question is, what is the best way to get the least significant digit in a number?
I'm still learning, so please go easy on me :)
Thanks!