30
\$\begingroup\$

Inspired by this. There is a number, given as either integer, string or array of digits (your choice). Find the base in which the representation of the number will have most "4"s and return that base.

Number    Result
624        5
444       10
 68       16

restrictions:

  • The base returned should not be greater than the input.
  • numbers less than or equal to abs(4) should not be considered valid input, so undefined returns are acceptable
\$\endgroup\$
  • \$\begingroup\$ This could be either code-golf or code-challenge. Could you please detail the requirements, winning criteria and perhaps give one or more examples of input and desired output? \$\endgroup\$ – codeporn Feb 1 '13 at 14:35
  • \$\begingroup\$ What is the highest acceptable base? \$\endgroup\$ – Steven Rumbalski Feb 1 '13 at 15:34
  • \$\begingroup\$ I would assume 36, as it gets difficult to represent after that \$\endgroup\$ – SeanC Feb 1 '13 at 15:35
  • 2
    \$\begingroup\$ @SeanCheshire: You don't actually have to display the number. You can easily represent a number in any base as an array, such as [1,15,3,64,43] for some number in base 80. You're only outputting the base number, so you could technically test every base from 2 to n. \$\endgroup\$ – mellamokb Feb 1 '13 at 18:27
  • 1
    \$\begingroup\$ What is the correct answer for 1, 2, and 3, 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., 4 in any base > 5, 44 in any base > 45, 14 in base 9, or any base > 15, etc). Should the correct answer be the smallest base with the largest number of "4"s? \$\endgroup\$ – mellamokb Feb 1 '13 at 18:31

19 Answers 19

24
\$\begingroup\$

APL (31 19)

Now tests all possible bases.

⊃⍒{+/4=K⊤⍨K⍴⍵}¨⍳K←⎕

Explanation:

  • ⍳K←⎕: read user input, store in K. Make a list from 1 to K, which are the bases to try.
  • {...: for each of these, run the following function
  • K⊤⍨K⍴⍵: encode K into that base giving a list of digits (as numbers) per base. Use K digits (a big overestimate, but it doesn't matter because the unused ones will all be zero anyway).
  • 4=: see which of these are equal to 4
  • +/: sum these, now we know how many fours per base
  • ⊃⍒: give the indices of the list if it were sorted downwards, so the index of the biggest one is at the front. Take the first item of this list.
\$\endgroup\$
  • 2
    \$\begingroup\$ I love your APL solutions. \$\endgroup\$ – MrZander Feb 1 '13 at 22:10
  • 25
    \$\begingroup\$ Funny how that APL expression contains the expression most people make when reading it: \$\endgroup\$ – epidemian Feb 2 '13 at 1:34
5
\$\begingroup\$

GolfScript, 30 characters

.,{[2+.2$\base{4=},,\]}%$)~p];

Works for any base - test the code online.

Comment: This solution was based on the original version of the question. It thus may return a base larger than the input, e.g. for the input 4 it correctly returns base 5 - which is no longer valid by the new rules.

\$\endgroup\$
5
\$\begingroup\$

GolfScript (23 chars)

~:^,2>{^\base[4]/,~}$0=

or

~:^,2>{^\base[4]/,}$-1=

or

~:^,2>{^\base[4]/,}$)\;

Note that this takes input from stdin: for a fair comparison with Howard's GolfScript version subtract one char.


Howard points out that the rules have changed, and it's not very logical that they now exclude 4 as a possible input when it has a valid output (any integer greater than 4). To cover that case as well requires an extra 2 characters, which can be added in all kinds of ways:

~:^)),2>{^\base[4]/,}$)\;

or

~:^,{))^\base[4]/,}$)))\;

being a couple of the obvious ones.

\$\endgroup\$
  • \$\begingroup\$ Nice. But gives wrong answer for input "4". \$\endgroup\$ – Howard Feb 2 '13 at 18:24
  • \$\begingroup\$ I just saw that they changed the rules completely and removed any special cases after I did my submission. Thus your solution conforms to the new rules. \$\endgroup\$ – Howard Feb 2 '13 at 18:53
  • \$\begingroup\$ @Howard, the rules may say that that case doesn't need to be handled, but in the interests of completeness I'll add some variants. \$\endgroup\$ – Peter Taylor Feb 2 '13 at 19:27
  • \$\begingroup\$ Nevertheless, I can't +1 more than once ;-) \$\endgroup\$ – Howard Feb 2 '13 at 19:36
  • \$\begingroup\$ @Howard, you can add a bounty if you really want ;) \$\endgroup\$ – Peter Taylor Feb 2 '13 at 22:04
4
\$\begingroup\$

Python 2.x, 77 chars

F=lambda x:max((sum(x/b**d%b==4for d in range(99)),b)for b in range(5,99))[1]

Works up to base 98 and numbers at most 98 digits long.

\$\endgroup\$
4
\$\begingroup\$

J, 38 characters

f=.[:(i.>./)[:+/[:|:4=(10#"0(i.37))#:]

Usage:

   p 624
5
   p 444
10
   p 68
16
\$\endgroup\$
4
\$\begingroup\$

VBA, 121

Function k(a)
For w=5 To a
Z=0:q=a:Do:c=q Mod w:Z=Z-(c=4):q=Int(q/w):Loop Until q=0
If Z>x Then x=Z:k=w
Next
End Function

usage:

  • direct window: ?k(num)
  • Excel formula: =k(A1)
\$\endgroup\$
  • \$\begingroup\$ fixed for all bases, and reduced test to simply counting 4s \$\endgroup\$ – SeanC Feb 1 '13 at 19:52
  • \$\begingroup\$ FWIW, you can remove a space: For w=5To a \$\endgroup\$ – Engineer Toast Dec 3 '18 at 19:02
3
\$\begingroup\$

Mathematica 59

Code

Sort[{Count[IntegerDigits[n, #], 4], #} & /@ Range[5, 36]][[-1, 2]]

Let's give the above function a name.

whichBase[n_] := Sort[{Count[IntegerDigits[n, #], 4], #} & /@ Range[2, 36]][[-1, 2]]

Explanation

  1. Count[IntegerDigits[n,k],4]: Count the number of fours in the base k representation of n.
  2. Sort the bases from fewest to most 4s.
  3. Return the base from the last item in the list, that is, the base that had the representation with the most 4's.

Some special numbers

Now let's apply whichBase to the following special numbers.

numbers= {1953124, 8062156, 26902404, 76695844, 193710244, 444444444, 
943179076, 1876283764, 3534833124, 6357245164, 10983816964, 
18325193796, 29646969124, 46672774204, 71708377284, 107789473684, 
158856009316, 229956041484, 327482302084, 459444789604, 635782877604, 
868720588636, 1173168843844, 1567178659764, 2072449425124, 
2714896551724, 3525282954756, 4539918979204, 5801435550244, 
7359635486844, 9272428079044, 11606852190676}

{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, \ 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}

If you convert each number to the corresponding base, you will see what is special about them.

\$\endgroup\$
  • \$\begingroup\$ I think you'd have to add 7 bytes for a complete function definition if you want to use n there. Also, MaximalBy really helps, knocks it down to 49 bytes: MaximalBy[Range[a=#],a~IntegerDigits~#~Count~4&]& (just ignore messages from it trying to use base-1) \$\endgroup\$ – LegionMammal978 Dec 3 '18 at 11:39
  • \$\begingroup\$ Also, the current one fails starting on n = 152, where it gives 36 instead of 37. \$\endgroup\$ – LegionMammal978 Dec 3 '18 at 11:46
  • \$\begingroup\$ Although your code works, I do not understand how it knows which bases to use. Shouldn't it need to be told to examine bases 2 through 36 (or 1 through 36)? \$\endgroup\$ – DavidC Dec 5 '18 at 15:36
  • \$\begingroup\$ Base 36 is never specified in the problem, and that's why I claim that yours fails for n = 152 = 4·37 + 4. My code checks all bases from 1 to n, since bases n + 1 and onward will only contain the single digit n. \$\endgroup\$ – LegionMammal978 Dec 5 '18 at 21:24
  • \$\begingroup\$ Thanks for the clear explanation. \$\endgroup\$ – DavidC Dec 5 '18 at 21:46
3
\$\begingroup\$

Japt -h, 10 bytes

444 in base 10 is [4,4,4] which contains the number and digit 4 3 times but 444 in base 100 is [4,44] which also contains the digit 4 3 times, but only as a number once. Given the expected output in the challange for the 444 test case, I'd guess we're supposed to be counting the number 4:

õ ñ@ìX è¥4

Try it

But if we are counting the digit 4 then:

õ ñ@ìX ¬è4

Try it

õ              :Range [1,...,input]
  ñ@           :Sort by passing each X through a function
    ìX         :  Convert the input to a base X digit array
               :(VERSION 1)
       è       :  Count the elements
        ¥4     :    Equal to 4
               :(VERSION 2)
       ¬       :  Join to a string
        è4     :  Count the occurrences of "4"
               :Implicitly output the last element in the sorted array
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ @AttilaO. I was hoping someone would notice :) \$\endgroup\$ – Gordon Bailey Feb 9 '13 at 16:45
2
\$\begingroup\$

R - 148 137 chars

(so, far away from the rest of the competition but still)

f=function(n){s=sapply;which.max(s(lapply(strsplit(s(4:n,function(x){q=n;r="";while(q){r=paste(q%%x,r);q=q%/%x};r})," "),`==`,4),sum))+3}

Basically transform the input from base 10 to all bases from 4 to n (using modulo %% and integer division %/%) and pick the index of the first one having the most 4s.

f(624)
[1] 5
f(444)
[1] 10
\$\endgroup\$
2
\$\begingroup\$

J translation of @marinus' APL solution:

NB. Expression form (22 characters, not including "n" - the "argument"):
{.\:(+/@(4=$#:[)"0 i.)n
NB. Function form (24 characters, not including "f=:"):
f=:{.@\:@(+/@(4=$#:[)"0 i.)

Just for interest, here are some values:

(,.f"0)9+i.24
 9  5
10  6
11  7
12  8
13  9
14  5
15 11
16  6
17 13
18  7
19  5
20  5
21  5
22  5
23  5
24  5
25  6
26  6
27  6
28  6
29  5
30  7
31  7
32  7

It outputs the smallest base that gives a fouriest transform. For the last few values in the table, the representations look like “4n” (e.g 31 in base 7 is “43”).

\$\endgroup\$
2
\$\begingroup\$

Jelly, 6 bytes

bⱮċ€4M

Try it online!

Outputs "all" bases up to N which gives the most 4's. If you want maximal or minimal base, add (max) or (min) respectively.

How it works

bⱮċ€4M    Main link (monad). Input: integer N.
bⱮ        Convert N to each base of 1..N
  ċ€4     Count 4's in each representation
     M    Take maximal indices
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 10 9 bytes

LBε4¢}Zk>

-1 byte thanks to @Cowabunghole.

If multiple bases have the same amount of 4s, it will output the smallest one (i.e. 16 will result in 6, but 12 would also have been a possible output).

Try it online or verify all test cases.

Explanation:

L           # Create a list in the range [1, (implicit) input]
            #  i.e. 624 → [1,2,3,...,622,623,634]
 B          # Convert the (implicit) input integer to Base-N for each N in this list
            #  i.e. 624 and [1,2,3,...,622,623,624]
            #   → ["1","1001110000","212010",...,"12","11","10"]
  ε  }      # Map this list to:
   4¢       #  Count the number of 4s in the number
            #   → [0,0,0,0,4,0,0,0,0,1,0,2,...,0,0,0]
      Z     # Take the max (without popping the list)
            #  i.e. [0,0,0,0,4,0,0,0,0,1,0,2,...,0,0,0] → 4
       k    # Get the index of this max in the list
            #  i.e. [0,0,0,0,4,0,0,0,0,1,0,2,...,0,0,0] and 4 → 4
         >  # Increase it by to convert the 0-index to our base (and output implicitly)
            #  i.e. 4 → 5
\$\endgroup\$
  • \$\begingroup\$ Couldn't you replace Qƶà with k>? i.e. find the 0-based index of the max and increment it? \$\endgroup\$ – Cowabunghole Dec 3 '18 at 14:59
  • \$\begingroup\$ @Cowabunghole Ah, you're indeed right. Not sure how I missed that. Thanks! \$\endgroup\$ – Kevin Cruijssen Dec 3 '18 at 15:06
1
\$\begingroup\$

C# with Linq 273

using System;using System.Linq;class P{static void Main(){int r,z,k=int.Parse(Console.ReadLine());if(k<=4) return;Console.WriteLine(Enumerable.Range(4, k).Select(x =>{r = 0;z = k;while (z > 0){if(z % x==4){r++;}z/=x;}return new[]{r, x};}).OrderBy(n => n[0]).Last()[1]);}}

or

using System;
using System.Linq;

class P
{
    static void Main()
    {
        int r, z, k = int.Parse(Console.ReadLine());
        if (k <= 4) return;
        Console.WriteLine(
            Enumerable.Range(4, k).Select(x =>
                {
                    r = 0;
                    z = k;
                    while (z > 0)
                    {
                        if (z % x == 4)
                        {
                            r++;
                        }
                        z /= x;
                    }
                    return new[] { r, x };
                }).OrderBy(n => n[0]).Last()[1]);

    }
}

Pretty sure the number of variables can be reduced and the if's can be converted to ?s. Oh well...

\$\endgroup\$
1
\$\begingroup\$

C# (482 ~423 Bytes)

First attempt at a 'golfed' solution. I used basically the same algorithm as the VBA above. I could probably save some bytes inlining the conversion function, or shortening the name. Like I said this is a first attempt, so please be gentle.

With whitespace:

using System;
class Program
{
    static void Main(string[] args)
    {
        int n = int.Parse(args[0]);
        int c=0, m=0;
        string r="";
        int t = 0;
        for (int i = 5; i < 37; i++)
        {
            while (n > 0)
            {
                r = (char)((int)(n % i) + 48 + (7 * ((int)(n % i) > 9 ? 1 : 0))) + r;
                n = (int)(n / i);
            }
            t = r.Length - r.Replace("4", "").Length;
            if (t > c) { c = t; m = i; }
        }
        Console.WriteLine("Base: " + m);
    }
}
\$\endgroup\$
  • 4
    \$\begingroup\$ I don't think the namespace is required. All names should be a single character, including Program and cBase. And yes, you should inline cBase. Also, combine declaration and initialization, i.e., int c=0,m=0. \$\endgroup\$ – mellamokb Feb 1 '13 at 18:22
  • 2
    \$\begingroup\$ Also, it looks like you've combined your test code with the function code that performs the logic. The spec requires an input of a number/string of digits, and the output of an integer. It would be fair to simply create a function that takes int parameter, and returns int parameter, without even a Main method, and call the character count your score. \$\endgroup\$ – mellamokb Feb 1 '13 at 18:29
  • \$\begingroup\$ @mellamokbtheWise - I learned something new. I always assumed the namespace was required. Also, good catch on the test array, that saves me some chars, and I am now actually answering the challenge. \$\endgroup\$ – theB Feb 7 '13 at 1:34
1
\$\begingroup\$

Burlesque - 28 bytes

Jbcjro{dg}Z]J{4CN}Cmsb[~Fi?i
Jbcjro                        create a list 1..input and convert input
                              to an infinite list.                      
      {dg}Z]                  convert number to base (zipWith operation)
            J                 duplicate
             {4CN}Cm          create comparison function 
              4CN             count the number of fours.
                    sb        sort by 
                      [~      take the last element (which is going to be
                              the base-n representation where count of fours
                              is highest)
                        Fi    Find the index of this last element in the original
                              unsorted list
                          ?i  increment (because base 1 is index 0)

Try it online.

\$\endgroup\$
1
\$\begingroup\$

k, 18 bytes

{*>+/'4=x{y\x}'!x}

Try it online!

{                } /function(x)
        x     '!x  /for every y in 0, 1, ..., (x-1):
         {y\x}     /    do x in base y
      4=           /see which digits equal four
   +/'             /sum, to get number of 4s per base
 *>                /get index (which equals base) of largest number of 4s
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 44 bytes

{max 5..$^a,by=>{grep 4,$a.polymod($_ xx*)}}

Try it online!

Good old polymod.

\$\endgroup\$
1
\$\begingroup\$

Husk, 9 bytes

S€►#4ṠMBḣ

Try it online!

 €           The 1-based index in
      MB     the list of the input's representations in every base
     Ṡ  ḣ    from 1 to itself
S ►          of its element which has the largest
   #         number of
    4        fours.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.