|
|
C - (114 characters)
In all it's golfy glory:
x,k,c,d,n;main(v){scanf("%d",&v);for(k=5;v/k;++k){x=v;c=0;while(x)c+=x%k==4,x/=k;c>=d?n=k,d=c:0;}printf("%d",n);}
And somewhat ungolfed:
x,k,c,d,n; // declare a bunch of ints, initialized to 0
main(v){ // declare one more, without using an extra comma
scanf("%d",&v); // get the input (v)
for(k=5;v/k;++k){ // loop over each base (k) greater than or equal to (/)
// our input (v)
x=v; // temp value (x) set to input (v)
c=0; // number of 4s in the current base (c) re-initialized
while(x) // loop over our temp until it's used up
c+=x%k==4, // if the next digit (x%k) is 4 (==4) increment the
// current count (c+=)
x/=k; // remove the current digit
c>=d?n=k,d=c:0; // if the number of 4s in this base (c) is greater
// than the current maximum number of 4s (d), then
// save the new best base (n), and new maximum
// number of 4s
}
printf("%d",n); // output the result
}
Just for fun here's the output for the numbers [0,127] (these are the largest bases under the input number itself).
0, 0, 0, 0, 0, 5, 6, 7, 8, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 5, 21, 22, 23, 6, 25, 26, 27, 7, 29, 30, 31, 8, 33, 34, 35, 9, 37, 38, 39, 10, 41, 42, 43, 11, 5, 46, 47, 12, 49, 50, 51, 13, 53, 54, 55, 14, 57, 58, 59, 15, 61, 62, 63, 16, 65, 66, 67, 17, 69, 5, 71, 18, 73, 74, 75, 19, 7, 78, 79, 20, 81, 82, 83, 21, 85, 86, 87, 22, 89, 90, 91, 23, 93, 94, 5, 24, 97, 98, 99, 25, 101, 102, 103, 26, 5, 106, 107, 27, 109, 5, 111, 28, 113, 114, 5, 29, 9, 5, 5, 5, 121, 122, 123
|
[1,15,3,64,43]for some number in base80. You're only outputting the base number, so you could technically test every base from2ton. – mellamokb Feb 1 at 18:271,2, and3, which have the same number of "4"s (0) in every base? Also, many numbers have the same number of "4"s in many bases (e.g.,4in any base > 5,44in any base > 45,14in base 9, or any base > 15, etc). Should the correct answer be the smallest base with the largest number of "4"s? – mellamokb Feb 1 at 18:31