Skip to content

Instantly share code, notes, and snippets.

@aeurielesn
Last active December 15, 2015 13:28
Show Gist options
  • Save aeurielesn/6cff01aac21367d6520c to your computer and use it in GitHub Desktop.
Save aeurielesn/6cff01aac21367d6520c to your computer and use it in GitHub Desktop.
#include <stdio.h>
int addPrefix(int N, int digit) {
int multiplier = 0;
// [1, 5]
if(N <= 100000) {
// [1, 3]
if(N <= 1000) {
//[1, 2]
if(N <= 100) {
//[1, 1]
if(N <= 10) {
multiplier = 10;
//[2, 2]
} else {
multiplier = 100;
}
//[3, 3]
} else {
multiplier = 1000;
}
//[4, 4]
} else if(N <= 10000) {
multiplier = 10000;
//[5, 5]
} else {
multiplier = 100000;
}
//[6, 7]
} else if(N <= 10000000) {
//[6, 6]
if(N <= 1000000) {
multiplier = 1000000;
//[7, 7]
} else {
multiplier = 10000000;
}
//[8, 9]
} else {
//[8, 8]
if(N <= 100000000) {
multiplier = 100000000;
//[9, 9]
} else {
multiplier = 1000000000;
}
}
return N + digit * multiplier;
}
int main(void) {
int i, j, k, N = 2;
for(i = 1; i < 9; ++i, N *= 10) {
for(j = 1; j < 50000000; ++j) {
k = addPrefix(N, 9);
}
}
return 0;
}
#include <iostream>
int add_prefix( int number, int prefix )
{
// without the above, the result of (X) would be "90"
int tmp = 10;
while( number >= tmp ) tmp *= 10;
return number + prefix * tmp;
}
int main(void) {
int i, j, N = 2, k;
for(i = 1; i < 9; ++i, N *= 10) {
for(j = 1; j < 50000000; ++j) {
k = add_prefix(N, 9);
}
}
return 0;
}
#include <stdio.h>
#include <math.h>
int addPrefix(int x, int addition) {
int y = 1;
while (y <= x)
{
y *= 10;
}
x += addition * y;
return x;
}
int main(void) {
int i, j, N = 2, k;
for(i = 1; i < 9; ++i, N *= 10) {
for(j = 1; j < 50000000; ++j) {
k = addPrefix(N, 9);
}
}
return 0;
}
#include <stdio.h>
#include <math.h>
int addPrefix(int N, int digit) {
int noDigits = floor(log10(N)) + 1;
//Add 9 in front
return N + digit * pow(10, noDigits);
}
int main(void) {
int i, j, N = 2, k;
for(i = 1; i < 9; ++i, N *= 10) {
for(j = 1; j < 50000000; ++j) {
k = addPrefix(N, 9);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment