13
\$\begingroup\$

Tom is going to implement a new programming language of his invention. But before actually starting working on it, he wants to know whether his language should be case sensitive or not.

On one hand, case insensitivity seems more easy to implement to him, but he worries that it could cause a lack in the possibilities of combinations of characters that form a variable, meaning that longer variable names should then be used to avoid naming clashes (for an example, you can use Hello, HEllo, heLLo and a bunch of other possibilities if the language is case sensitive, but only HELLO if not).

But Tom is a meticulous person, so just a worry isn't enough for him. He wants to know the numbers.

The challenge

Write a function (or a full program if your language doesn't support them) that, given an integer n as the input, outputs (or returns) the difference in the number of permutations possible for a string of length n with case sensitivity and without.

In Tom's language, variable names can include all alphabet letters, underscores and, starting from the second character, digits.

Testcases

Input (length of the variable) -> Output (difference between the possibilities with case sensitivity and the possibilities with case insensitivity)
0 -> 0
1 -> 26
2 -> 2340
5 -> 784304586
8 -> 206202813193260
9 -> 13057419408922746

Non-competing C++ reference implementation

void diff(int n) {
    long long total[2] = {0, 0}; //array holding the result for case insensivity ([0]) and case sensitivity ([1])

    for (int c = 1; c <= 2; c ++) //1 = insensitivity, 2 = sensitivity
        for (int l = 1; l <= n; l ++) //each character of the name
            if (l == 1)
                total[c - 1] = 26 * c + 1; //first character can't be a number
            else
                total[c - 1] *= 26 * c + 1 + 10; //starting from the second character, characters can include numbers

    std::cout << total[1] - total[0] << std::endl;
}

Scoring

Tom likes golf, so the shortest program in bytes wins.

Note

It's okay if maybe the last two testcases aren't right because of numerical precision. After all, I am not even sure my code handled number 9 correctly.

\$\endgroup\$
12
  • 4
    \$\begingroup\$ Test case 5 seems to be missing its last three digits. \$\endgroup\$
    – Dennis
    Commented Jul 11, 2016 at 5:15
  • 1
    \$\begingroup\$ The spec says that only even numbers can be used, but the outputs imply that 10 digits are allowed? \$\endgroup\$
    – feersum
    Commented Jul 11, 2016 at 8:15
  • \$\begingroup\$ @Dennis Oh, thanks, I forgot ;) \$\endgroup\$ Commented Jul 11, 2016 at 9:25
  • 19
    \$\begingroup\$ "case insensitivity seems more easy to implement" makes me think Tom didn't really think this through.. \$\endgroup\$
    – pipe
    Commented Jul 11, 2016 at 9:50
  • 2
    \$\begingroup\$ @dorukayhan I can't comment on your question as I'm not part of the site, but anyway in this case no, C++ isn't banned as the code given is higly golfable and there even are other, shorter approaches to the challenge. \$\endgroup\$ Commented Jul 11, 2016 at 19:13

11 Answers 11

11
\$\begingroup\$

JavaScript (ES7), 43 bytes

f=(n,a=53,b=27)=>n?--n?f(n,a*63,b*37):a-b:0
<input type=number value=0 min=0 max=9 style=width:4ch oninput=o.textContent=f(+this.value)><div id=o>0</div>

Not reduces day today - 55 bytes.

\$\endgroup\$
3
  • \$\begingroup\$ Today ain't your day, pancakes! \$\endgroup\$
    – cat
    Commented Jul 11, 2016 at 21:02
  • \$\begingroup\$ Seems you have the wrong formula. This doesn't give the correct result. Try 2 as input. \$\endgroup\$ Commented Jun 13, 2019 at 11:15
  • \$\begingroup\$ @t-clausen.dk Sorry, I had a typo in it. Fixed now. \$\endgroup\$
    – Neil
    Commented Jun 13, 2019 at 12:23
8
\$\begingroup\$

TSQL, 41 bytes

DECLARE @ INT=10

PRINT 53*POWER(63.,@-1)-27*POWER(37.,@-1)

Fiddle

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

Jelly, 16 15 bytes

“~J“6j”OH*"’ÆḊḞ

Try it online! or verify all test cases.

How it works

“~J“6j”OH*"’ÆḊḞ  Main link. Argument: n

“~J“6j”          Yield ['~J', '6j'].
       O         Ordinal; replace the characters with it code points.
                 This yields [[126, 74], [54, 106]].
        H        Halve, yielding [[63, 37], [27, 53]].
           ’     Yield n-1.
         *"      Exponentiation (zipwith); elevate 63 and 37 to the power n-1.
            ÆḊ   Take the determinant of the matrix
                 [[63 ** (n-1), 37 ** (n-1)], [27, 53]], thus yielding
                 63 ** (n-1) * 53 - 37 ** (n-1) * 27.
              Ḟ  Floor; round the result down to the nearest integer.
                 This is required for n = 0.
\$\endgroup\$
5
\$\begingroup\$

Java, 77 76 Bytes

O(1) solution, no loop involved

long x(long n){return 53*(long)Math.pow(63,n-1)-27*(long)Math.pow(37,n-1);}
\$\endgroup\$
7
  • \$\begingroup\$ Nice but wrong for input 0 \$\endgroup\$
    – edc65
    Commented Jul 11, 2016 at 10:21
  • 3
    \$\begingroup\$ My fault, sorry. It seems wrong as Math.pow for exponent < 0 will not give you an integer result. I did not think about the (long) conversion. Upvote \$\endgroup\$
    – edc65
    Commented Jul 11, 2016 at 11:07
  • 1
    \$\begingroup\$ There's an extra space after n-1 \$\endgroup\$
    – cat
    Commented Jul 11, 2016 at 12:21
  • 6
    \$\begingroup\$ the pow function isn't constant time \$\endgroup\$ Commented Jul 11, 2016 at 19:41
  • 1
    \$\begingroup\$ You can probably just cast the entire return value to long. \$\endgroup\$ Commented Jul 12, 2016 at 5:54
4
\$\begingroup\$

JavaScript (ES6) 47

n=>n&&eval("for(a=53,b=27;--n;a*=63)b*=37;a-b")

Note: the values are correct up to the 53 bits of precision of javascript numbers.

Test

F=n=>n&&eval("for(a=53,b=27;--n;a*=63)b*=37;a-b")

for(i=0;i<=10;i++)O.textContent+=i+' '+F(i)+'\n'
<pre id=O></pre>

\$\endgroup\$
3
\$\begingroup\$

MATL, 17 16 bytes

37iqY"27vt26+hpd

Try it online!

Last test case (input 9) is not exact due to numerical precision.

Explanation

37      % Push number 37
iq      % Input number n and subtract 1
Y"      % Row array [37 ...37] of size n-1
27v     % Concatenate 27 vertically. Gives column array [37; ... 37; 27]
t26+    % Duplicate and add 26. Gives column array [63; ... 63; 53]
h       % Concatenate horizontallty. Gives 2D array [37 63; ... 37 63; 27 53]
p       % Product of each column
d       % Difference of the two values. Implicitly display
\$\endgroup\$
3
\$\begingroup\$

Python 3, 36 bytes

lambda n:n and 53*63**~-n-27*37**~-n

An anonymous function that takes input via argument and returns the output.

Hooray for operator precedence!

How it works

lambda n:   Function with input string length n
n and ...   Base case: return 0 if n=0

(Case sensitive)

53*...      The first character can be any of [a-z], [A-Z] and _ , giving 53 possibilities
...63**~-n  63^(n-1): the remaining n-1 characters can be any of above and [0-9], giving
            63 possibilities

(Case insensitive)

27*...      The first character can be any of [A-Z] and _ , giving 27 possibilities
...37**~-n  37^(n-1): the remaining n-1 characters can be any of above and [0-9], giving
            37 possibilities
...-...     Subtract the case insensitive value from the case sensitive value
:...        Return the above

Try it on Ideone

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is actually quite good, congratulations. \$\endgroup\$ Commented Jul 12, 2016 at 6:06
2
\$\begingroup\$

Pyth, 16 bytes

L*b^+TbtQ-y53y27

Try it here!

Works the same as my Pyke answer

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

Factor, 54 bytes

[ 1 - [ 63 swap ^ 53 * ] [ 37 swap ^ 27 * ] bi - ⌊ ]

Test output (too lazy to make a test suite right now)

enter image description here

To 100 because I can:

0   0
1   26
2   2340
3   173394
4   11884860
5   784304586
6   50726849940
7   3244471004034
8   206202813193260
9   13057419408922746
10  825083183338785540
11  52071473691679633074
12  3283878468805254213660
13  207009241705220196670506
14  13046203459736972095009140
15  822081803558828931061964514
16  51797480091236011152730146060
17  3263475325027970876967575323866
18  205607606410125945699743561920740
19  12953599658372394455762938112142354
20  816088635295235866150191568376990460
21  51414022725857535138635748098295218826
22  3239099663712558709867478263942757120340
23  204063879397281956578587897577149556950594
24  12856046623614221305157697924464388847546860
25  809931759486357889731081403194103267502851386
26  51025731268991039110785546457483836346653407940
27  3214622195536403670115403894902928918116194213234
28  202521239965622254844299280697898069287570327415260
29  12758839658766868529390921590771079620628184030477546
30  803806955516821376897030535770307562553491969842783540
31  50639840307096567147692815348943369659664900009235194274
32  3190310017399946085222303356013750037654300219219990995660
33  200989533984152510500958383022988009088751140000471861713306
34  12662340747855976725442649179430749571102940189045387160447140
35  797727471068538170566530927116489907924415111563438806390997714
36  50256830823601535309646277474395913542200557575679083168097488060
37  3166180347299391055374044156331053378848244132513532459112333134666
38  199469362080123926730618942840288463417954913542445303046398068798740
39  12566569818457512122984997355621160915700234281355486142448958342967554
40  791693898836982339089426979801403683342770524668108612843741935926892460
41  49876715636873773150264669144187442239779806356971223086325671695231877626
42  3142233085498371482609012624501672238105982542695761132093804716877601438340
43  197960684400284383047634318675066295949671525651476192195155330966174637087794
44  12471523117731734378801823439793231607942107258123800020604874301575774756008860
45  785705956436110540996146747147743624935526399518788998053580353675424396549238186
46  49499475256178381262627624276616339615439587933192321957328073370569308698667165940
47  3118466941165264455200744360060243571819246756040935041269911532639516601510442382434
48  196463417294374638796889443817231669538334996131822201644459414237154601574451074437260
49  12377195289581232434616009278422739187922835424850800583245777641132746959324266250272346
50  779763303244935960426051634304306897098424666501802506291342869526467319662381170513981540
51  49125088104479743237515245802427284662794337274853034469588379271166298804053252422013315474
52  3094880550584028599998398220679389089143005829869601804793717698250434558272314733733240577660
53  194977474686860578513191784102480908365326982799277757130761257743954813314983341977611019396106
54  12283580905274687184722912157844434869740351690501733906101969819773718376165568553428918153085140
55  773865597032396709958041167041560489574457972145056938738392490176213167779330685193080529096190914
56  48753532613044375168215008464220671276081037423946152138715557367728779243091128221265244694777630060
57  3071472554621920785909306886048189626410042209324487489672362842172125030215492994941453756154541985466
58  193502770941185640073821503874720577896459322697230268331890319993036718605903879610376090231927542876740
59  12190674569294866655427556036093727770484123879787646494762754214200448415158073789829080317596516919552754
60  768012497865582938530677678077399109971765706771520893994639085516276250445445435611121365429028589509594460
61  48384787365531959679266134687605431564178077913367085420171928478326379788811073557020550322585378011481976426
62  3048241604028522138211603801162125831073621928852293338115689139494198039092774045292531129443472159001332956340
63  192039221053797215808791020159404322131263093269170657697148148666441012621558792067838210142400699755368808584994
64  12098470926389236476707853555431516900893696610762369998567143482483125633030622907206930951507336372904773737270860
65  762203668362522337620493487551580215201395390665800196764662013706838562882208746410562227308797606160712680915464986
66  48018831106838923534843342117447155624156332266892935209806189913015690437626492644356866682916159530830240509968123940
67  3025186359730852784490963892261982080781180571047313262322191837350928353684220676552666116434808733122402791782863511634
68  190586740663043747689376558750428888318209646590603894258160955047853260958314713301270755405603305445874988549550654259260
69  12006964661771756928289219042180208601520032747949102211343066331920312103393552933082810823150791387679177947309210580307146
70  756438773691620716964985145770771121482256588592212543618533446975485259045523696603018951464617833773583196421918482952379540
71  47655642742572106296656344989755045898082462523751897107212657077842278391541997773285863117697288652678155847014078432541996674
72  3002305492782042738420254124183837105633106144366742271546464242884371700319084040546949135905704694741593153841915859492202959660
73  189145246045268694064519472247264698574880394293808554997733720639986819101224007245165566663218089624766534104801769122964901718906
74  11916150500851927783194334861253945564257269006861979264798563913835211476678615826001618230525611319033999768874671043820165072923140
75  750717481553671452455038596317020544047680701587330184780139088571711872342908421711680887160599764988069185889174180556385321385544114
76  47295201337881301582877865070119107294484376103737744811768456374021309282152988680758316620704776514138665127670559852493727353570572060
77  2979597684286522002615091538996935841273293894973707820454341421941470553783979337065903514099119599226677240459390970372438551207353876266
78  187714654110050886271820850421245945023886308799558098889201870386303383441699317091742715367049125868210495633339022021080976659562384154740
79  11826023208933205839086306664722737056380582810772096959441117184084770483299475874473650445339864801023664915823061850169943403091896529897954
80  745039462162791968009016264140349407787379295265439767433382158538003861527346479306512775013394967703567826260992924685854583715749735525096460
81  46937486116255893989991445578566240700314790994634218730880971697698786156163569667253198313252271919170619707315735295839320298967762741317515226
82  2957061625324121321570127646145469600479097962199681775200883358217791614396112323461838552769003522200045367717199022141204995255363640622751674340
83  186294882395419643266342704970909052975476018411475813658404790794443290909593951451816650008709306376417822233966923239519043908642509112264039442194
84  11736577590911437526054302953925815658830824491260123147847219971438656837802057319192119334366383427393476471247901403340755546923998264934769791332860
85  739404388227420564151585450105392563397247850208862193146980429802018372669942231085027322266336949575917703497413242262756663341354137752789503193531786
86  46582476458327495541925964824938180038990133131758872257066173226778328178077627497465902058225655188337576695262466055088365154255573819716001935756281940
87  2934696016874632219155250798298147938620028574339029453481005940809327991114127409087391517619734089372293502673776337794350733176860886869847861631505600834
88  184885849063101829807295655822883896191116857103773014116879344289312516128313791212146134064249406693214458990720825405024094143116346113226785650902666881260
89  11647808490975415277878675971455406774188399103593023755623709580904708065544348130631903778853340506994629744339509897103777855276371884029063199627227132581946
90  733811934931451162507061423422398315397346516450407479655925206552083331459335365747677739381568683697580070526580745691266622080032985612974682610468596757977540
91  46230151900681423237970948652777278349101493334565409591233653622119458645149661082264806229649731647953525118602676998977756027354075699625721942715793229749237874
92  2912499569742929663993134687280117361718934603610641124045403705739039618883255370668643812466536563096293367462807981691433106666850680498728209106577023932078141660
93  183487472893804568831603187418387900340137879398192142647374723980743503786481677084105117925340131838249669694817958084526161366922717594045263062187188374662340681706
94  11559710792309687836392321785788836463846951378802809804587636359996649027031299439381903065674516455247507129925990403129885565051842823161990850791287794680559919961140
95  728261779915510333692765148706621450691833741003095095947733154400551795376841154655141276683483798209790732933978380017958074358887427604479576881170443366718075867057314
96  45880492134677151022646012787988367271956130436320159940279535084902990655674156472314911622261081963797134173540354372100044183752897139657357014922552487271422413276314060
97  2890471004484660514426765717163702125632948593353801325373787525582612830543998915730356846276907462752691218884931833388144147044723120219693807782246945239322406481520807066
98  182099673282533612408888715907569328452365119288317839716587156275352411836023432836069625636200164217658002129970617297449211712144308789428081576440224676102555002804992632740
99  11472279416799617581760080704048343190386108757724073082212416905402170675604281811039500754948545126089276981776285626117157164453181285710701891703604838257394970772074262003154
100 722753603258375907650888473624290214416147782711338423841877029262370595570657559163071778136871929817566895212667053691361603944309744782913331015678320105744441365668988373398460
\$\endgroup\$
9
  • \$\begingroup\$ That's a language I never heard of. Are all that spaces really necessarry? \$\endgroup\$ Commented Jul 12, 2016 at 6:07
  • \$\begingroup\$ @user6245072 I linked the language, and yes, the spaces are needed :-) \$\endgroup\$
    – cat
    Commented Jul 12, 2016 at 12:22
  • \$\begingroup\$ @user6245072 like big numbers? :D \$\endgroup\$
    – cat
    Commented Jul 12, 2016 at 14:23
  • \$\begingroup\$ I prefer decimals u.u \$\endgroup\$ Commented Jul 12, 2016 at 14:31
  • 1
    \$\begingroup\$ @user6245072 I have to admit - the compilation error can be a bit hard to read when you mistype one of those... \$\endgroup\$ Commented Jul 15, 2016 at 8:19
2
\$\begingroup\$

dc, 19 bytes

1-d63r^53*r37r^27*-

This is just a difference of two exponentials, 53 × 63^(n-1) - 27 × 37^(n-1). The special case of zero falls out naturally, as dc starts with a precision of 0, meaning that x^-1 == 0 for all positive x.

As normal for dc, input is taken from top of stack, and output is pushed to top of stack. For a full program, enclose between ? and p to make a pipeline filter.

Test output

$ for i in {0..30}; do printf '%2d -> %s\n' "$i" `./85108.dc <<<$i`; done
 0 -> 0
 1 -> 26
 2 -> 2340
 3 -> 173394
 4 -> 11884860
 5 -> 784304586
 6 -> 50726849940
 7 -> 3244471004034
 8 -> 206202813193260
 9 -> 13057419408922746
10 -> 825083183338785540
11 -> 52071473691679633074
12 -> 3283878468805254213660
13 -> 207009241705220196670506
14 -> 13046203459736972095009140
15 -> 822081803558828931061964514
16 -> 51797480091236011152730146060
17 -> 3263475325027970876967575323866
18 -> 205607606410125945699743561920740
19 -> 12953599658372394455762938112142354
20 -> 816088635295235866150191568376990460
21 -> 51414022725857535138635748098295218826
22 -> 3239099663712558709867478263942757120340
23 -> 204063879397281956578587897577149556950594
24 -> 12856046623614221305157697924464388847546860
25 -> 809931759486357889731081403194103267502851386
26 -> 51025731268991039110785546457483836346653407940
27 -> 3214622195536403670115403894902928918116194213234
28 -> 202521239965622254844299280697898069287570327415260
29 -> 12758839658766868529390921590771079620628184030477546
30 -> 803806955516821376897030535770307562553491969842783540

Performance is reasonable for small n, but does start to tail off once it reaches a few hundred thousand or so:

   1024:   0.00s
   2048:   0.00s
   4096:   0.01s
   8192:   0.04s
  16384:   0.12s
  32768:   0.38s
  65536:   1.16s
 131072:   3.48s
 262144:  10.41s
 524288:  30.63s
1048576:  93.70s

That last example does produce a 1941435-digit result, so maybe it's not too bad for all that.

\$\endgroup\$
4
  • \$\begingroup\$ What's the size of its numbers? O.O \$\endgroup\$ Commented Jul 12, 2016 at 8:30
  • \$\begingroup\$ I mean, in my C++ implementation I used 8-bytes integers, which can hold up to 2 ^ 64 values, which is quite a bit. But it already fails to calculate testcase 11, as the result outceeds those bytes' capacity. So what size do the numbers in your language occupy, to be able to store the result of testcase 11 and up? \$\endgroup\$ Commented Jul 12, 2016 at 8:41
  • 1
    \$\begingroup\$ dc uses arbitrary-precision arithmetic, so it's just limited by available memory (although it does start to slow down once n reaches 100,000 or so). I'll add some figures. \$\endgroup\$ Commented Jul 12, 2016 at 8:50
  • \$\begingroup\$ Nice job, man! I was just looking through to see if dc had been used yet...but this can't be beaten! \$\endgroup\$
    – juh
    Commented Jul 12, 2016 at 22:54
1
\$\begingroup\$

Pyke, 17 15 bytes

53[DT+Qt^*)27[-

Try it here!

  [DT+Qt^*)     - [ = lambda a: a*(a+10)^(input-1) 
53[        27[- - [53)-[27)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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