[Question]
  [
Inspired by [this](http://www.smbc-comics.com/index.php?db=comics&id=2874). 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 4 should not be considered valid input, so undefined returns are acceptable
  
[Answer]
## 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.
[Answer]
### GolfScript, 30 characters
```
.,{[2+.2$\base{4=},,\]}%$)~p];
```
Works for any base - test the code [online](http://golfscript.apphb.com/?c=OzYyNAoKLix7WzIrLjIkXGJhc2V7ND19LCxcXX0lJCl%2BcF07Cg%3D%3D&run=true).
*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.
[Answer]
## 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.
[Answer]
## J, 38 characters
```
f=.[:(i.>./)[:+/[:|:4=(10#"0(i.37))#:]
```
Usage:
```
   p 624
5
   p 444
10
   p 68
16
```
[Answer]
## 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)`
[Answer]
## 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.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
bⱮċ€4M
```
[Try it online!](https://tio.run/##y0rNyan8/z/p0cZ1R7ofNa0x8f1/uB1I//9vZmSiY2JiomNmoWNoBgA "Jelly – 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
```
[Answer]
# 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.
[Answer]
# 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](https://ethproductions.github.io/japt/?v=1.4.6&code=9SDxQOxYIOilNA==&input=NDQ0Ci1o)
But if we *are* counting the digit 4 then:
```
õ ñ@ìX ¬è4
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.6&code=9SDxQOxYIKzoNA==&input=NDQ0Ci1o)
```
õ              :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
```
[Answer]
# 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
> 
> 
> 
[Answer]
**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
```
[Answer]
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”).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~10~~ 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
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](https://tio.run/##yy9OTMpM/f/fx@ncVpNDi2qjsu3@/zczMgEA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9TVnl4gr2SwqO2SQpK9v99nM5tNTm0qDYq2@6/zn8zIxMuExMTLjMLLkMzAA).
**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
```
[Answer]
# [k](https://en.wikipedia.org/wiki/K_programming_language), 18 bytes
```
{*>+/'4=x{y\x}'!x}
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqlrLTltf3cS2oroypqJWXbGi9n@aupmRiYKJiYmCmcV/AA "K (ngn/k) – 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
```
[Answer]
**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...
[Answer]
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);
    }
}
```
[Answer]
## 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.](https://tio.run/##SyotykktLixN/a9jZmTy3yspOasovzolvTYq1qvaxNmv1jm3OCm6zi3TPvP/fwA)
[Answer]
# [Perl 6](http://perl6.org/), 44 bytes
```
{max 5..$^a,by=>{grep 4,$a.polymod($_ xx*)}}
```
[Try it online!](https://tio.run/##DccxCoAgAAXQq3xEokIcwiQIO0phpC2JYosint1aHrxg4iOby@gsVCtOJ8yc012zM6ut3NEECEY1D/7Jzl89PZDSONTaXp1B/qoNxYIelcD6CDkJBiF@5LK2Dw "Perl 6 – Try It Online")
Good old [polymod](https://docs.perl6.org/routine/polymod).
[Answer]
# [Husk](https://github.com/barbuz/Husk), 9 bytes
```
S€►#4ṠMBḣ
```
[Try it online!](https://tio.run/##AR0A4v9odXNr//9T4oKs4pa6IzThuaBNQuG4o////zYyNA "Husk – 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.
```
[Answer]
# [Julia](http://julialang.org/), 50 bytes
```
n->sort(2:n+1,by=b->-sum(digits(n,base=b).==4))[1]
```
[Try it online!](https://tio.run/##LcrBCsIwDADQe76ixxbXYkccMkh/ZOyw4iaVmcmSgX59PejxwXsca5niuy5U2SfZdrVtz6fY5A9ln7wcT3sr96JiucmTzJRdIELnhjhWnUXFkBmguwIiQtcixP5yhhHgtRfWlYP9rUDJLH84V78 "Julia 1.0 – Try It Online")
]