Speedding - 1 year ago 59
C Question

# C Algorithm to find base system with the highest number of nulls in the end

As title says, I have to find the base system of given number which has the highest number of nulls in the end. For example if I have number 100 000, the answer is 10, because in 10 system there is the highest amount of nulls. Answer for number 54 would be 3, because in 3 numeral system it looks like 2000 (the highest amount of nulls in the end). I've written a program which works fine, but the problem comes if the number exceeds maximum of integer. I have no idea how to fix it.

#include <stdio.h>
#define NUM 29040838157

int convert(int number, int system)
{
int i=0;

while(number%system == 0)
{
i++;
number/=system;
}

return i;
}

int main()
{
int i,max;
int temp=0;

for(i=2;i<=NUM;i++)
{
if(convert(NUM,i) >= temp)
{
max=i;
temp = convert(NUM,max);
}
}

printf("%d\n",max);
}

Solved.

It's wise to use
unsigned long long int
or represent number in an array.

For larger number, you should use data type which can store higher integer values too. I recommend unsigned long long

#define NUM 29040838157ULL //note the ULL here

unsigned long long convert(unsigned long long number, unsigned long long system)
{
int i=0;

while(number%system == 0)
{
i++;
number/=system;
}

return i;
}

int main()
{
unsigned long long i,max;
unsigned long long temp=0;

for(i=2;i<=NUM;i++)
{
if(convert(NUM,i) >= temp)
{
max=i;
temp = convert(NUM,max);
}
}

printf("%llu\n",max);
}

And so on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download