Efficient way to determine if the number has identical digits in C
+1
A:
if its just for repeating occurances in decimal within the same number :-
int contains_repeat(int x)
{
int occurances = 0;
while(x>0)
{
if(occurances & 1 << (x % 10)) return 1;
occurances |= 1 << (x % 10);
x = x / 10;
}
return 0;
}
if its all the same
int all_the_same(int x)
{
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return 0;
x = x/10;
}
return 1;
}
if you want it for a different base, just pass "int base" in and use base instead of 10.
Keith Nicholas
2010-06-11 03:53:11