The 9 Billion Names of God is a short story by Arthur C. Clarke. It's about a group of Tibetan monks whose order is devoted to writing down all the possible names of God, written in their own alphabet. Essentially, they are devoted to writing every possible permutation of their alphabet, restricted by a few rules. In the story, the monastery hires some engineers to write a program to do all the work for them. Your goal is to write that program.
Rules:
The monk's alphabet uses 13 characters (according to my estimations). You may use
ABCDEFGHIJKLMor some other set of 13 characters.The minimum length of a possible name is 1 character. The maximum length is 9 characters.
No character may repeat more than 3 times in succession.
AAABAis a valid name, butAAAABis not.Your program should print out (to a file) every possible name in sequence from
AtoMMMLMMMLM, separated by any character not in the alphabet (newlines, semi-colons, whatever).This is code-golf, and you can use any language. The shortest solution by June 1st 2014 wins.
Edit: The names should start with A and end with MMMLMMMLM, progressing through all the billions of names sequentially. But the particular sequence is up to you. You can print out all the 1-letter names first, then all the 2-letter names, etc. Or you can print all the names starting with A, then all the ones starting with B, or some other pattern. But a human should be able to read through the file and confirm they are all there and in whatever logical order you choose, assuming they have the time.
11 Answers 11
Ruby, 46
?A.upto(?M*9){|s|s[/(.)1円{3}|[N-Z]/]||puts(s)}
My original, similar solution was longer and wrong (it output base13 numbers, which isn't quite all of them due to leading zeroes), but I'll leave it here because it got votes anyway.
1.upto(13**9){|i|(s=i.to_s 13)[/(.)1円{3}/]||puts(s)}
-
14\$\begingroup\$ Well I ran your code for about an hour and got up to 2 billion names and a 21 GB text file before seeing this and quitting it. I underestimated just how large the file would be. \$\endgroup\$CSturgess– CSturgess2014年05月30日 19:49:15 +00:00Commented May 30, 2014 at 19:49
-
2\$\begingroup\$ @CSturgess Well, Ruby isn't the fastest language for this sort of thing out there... \$\endgroup\$BenjiWiebe– BenjiWiebe2014年05月31日 13:33:50 +00:00Commented May 31, 2014 at 13:33
-
8\$\begingroup\$ @BenjiWiebe But still faster than being handwritten by monks! \$\endgroup\$Turophile– Turophile2014年06月01日 22:24:07 +00:00Commented Jun 1, 2014 at 22:24
-
1\$\begingroup\$ Accepting this one because it has more votes. \$\endgroup\$CSturgess– CSturgess2014年06月02日 14:21:05 +00:00Commented Jun 2, 2014 at 14:21
-
4\$\begingroup\$ Not posting this as a separate answer, as it requires an immensely huge amount of memory (~30 TB, if my calculation is correct), but in theory you can shorten this to 43 characters with
k=*?A..?M*9;puts k-k.grep(/(.)1円{3}|[N-Z]/)\$\endgroup\$Ventero– Ventero2014年06月02日 19:24:41 +00:00Commented Jun 2, 2014 at 19:24
C 140 (削除) 177 235 (削除ここまで)
Good old procedural style, no fancyness.
It counts (no write) 11,459,252,883 names in 8 minutes.
Next edit with the runtime and size of names file. Watch the sky...
Runtime 57 minutes, file size 126,051,781,713 (9 chars+crlf per row). Please tell me the monks' email address, so that I can send them the zipped file, for manual check...
Edit Golfed a little more, reworked the check for repeated letters.
Still not the shortest, but at least this one terminates and generates the required output.
Runtime 51 min, file size 113,637,155,697 (no leading blanks this time)
A side note: obviously the output file is very compressible, still I had to kill 7zip, after working 36 hours it was at 70%. Weird.
char n[]="@@@@@@@@@@";p=9,q,r;main(){while(p)if(++n[p]>77)n[p--]=65;else for(r=q=p=9;r&7;)(r+=r+(n[q]!=n[q-1])),n[--q]<65&&puts(n+q+1,r=0);}
Ungolfed
char n[]="@@@@@@@@@@";
p=9,q,r;
main()
{
while (p)
{
if (++n[p] > 77)
{
n[p--] = 65; // when max reached, set to min and move pointer to left
}
else
{
for (r=q=p=9; r & 7 ;) // r must start as any odd number
{
r += r+(n[q]!=n[q-1])); // a bitmap: 1 means a difference, 000 means 4 letters equal
n[--q] < 65 && puts(n+q+1,r=0);
}
}
}
}
-
\$\begingroup\$ no
#includes? \$\endgroup\$Simon Kuang– Simon Kuang2014年06月01日 04:33:44 +00:00Commented Jun 1, 2014 at 4:33 -
\$\begingroup\$ @SimonKuang, some compilers will put basic ones (stdio) in automatically. \$\endgroup\$Paul Draper– Paul Draper2014年06月01日 06:50:27 +00:00Commented Jun 1, 2014 at 6:50
-
1\$\begingroup\$ @Simon it's C standard. By default, global objects are int and global functions return int. Visual studio outputs C4013 warning about 'puts' not defined, but it's valid anyway. \$\endgroup\$edc65– edc652014年06月01日 07:15:21 +00:00Commented Jun 1, 2014 at 7:15
-
4\$\begingroup\$ fits into a tweet! \$\endgroup\$CincauHangus– CincauHangus2014年06月02日 09:47:05 +00:00Commented Jun 2, 2014 at 9:47
Golfscript, (削除) 58 (削除ここまで) 47 characters
"A"13
9?,{13base{65+}%n+}%{`{4円*/,}+78,1/%1-!},
Thanks to Peter Taylor, I am spared from the seppuku from not beating the Ruby solution! Run the code up to 10 yourself, and here is proof it skips the four-in-a-row numbers.
-
1\$\begingroup\$ Easy saving: use
n+instead of''+n. I think it's within the rules to use an alphabet with control characters, so you could also replace65+with13+and save another character by naming13:^. And I think that13,{ stuff [...]could be13,1/{ stuff 4*. \$\endgroup\$Peter Taylor– Peter Taylor2014年05月30日 19:35:19 +00:00Commented May 30, 2014 at 19:35 -
1\$\begingroup\$ My initial thought was that the saving would be via a filter, and with a bit of work it can be done. From
13,on can be replaced with{65+}%n+}%{ backtick {4円*/,}+78,1/%1-!},for a total saving of 8, saving your life. \$\endgroup\$Peter Taylor– Peter Taylor2014年05月30日 19:46:02 +00:00Commented May 30, 2014 at 19:46 -
\$\begingroup\$ As long as the character is something that you can physically see, it will work. Really you could even include newlines as a character. Just so long as there's a sequence to the characters. \$\endgroup\$CSturgess– CSturgess2014年05月30日 19:52:51 +00:00Commented May 30, 2014 at 19:52
-
\$\begingroup\$ @PeterTaylor: You are a gentleman and a scholar! \$\endgroup\$Claudiu– Claudiu2014年05月30日 20:07:56 +00:00Commented May 30, 2014 at 20:07
-
\$\begingroup\$ After
AAAMit should beAAABA, and notBAAAB, right? \$\endgroup\$justhalf– justhalf2014年06月02日 04:08:16 +00:00Commented Jun 2, 2014 at 4:08
Bash+Linux command line utils, 43 bytes
jot -w%x $[16**9]|egrep -v "[0ef]|(.)1円{3}"
This uses a similar technique to my answer below, but just counts in base 16, and strips out all "names" containing 0, e or f as well those with more than 3 same consecutive digits.
Convert to the monk's alphabet as follows:
jot -w%x $[16**9]|egrep -v "[0ef]|(.)1円{3}" | tr 1-9a-d A-M
Bash+coreutils (dc and egrep), 46 bytes
Edit - corrected version
dc<<<Edo9^[p1-d0\<m]dsmx|egrep -v "0|(.)1円{3}"
This'll take a while to run but I think its correct.
dc counts downwards from 14^9 to 1 and outputs in base 14. egrep filters out the numbers with more than 3 consecutive same digits. We also filter out any names with "0" digits, so we get the correct set of letters in the names.
The question specifies that any alphabet may be used, so I am using [1-9][A-D]. But for testing, this can be transformed to [A-M] using tr:
dc<<<Edo9^[p1-d0\<m]dsmx|egrep -v "0|(.)1円{3}" | tr 1-9A-D A-M
This yields the sequence:
MMMLMMMLM MMMLMMMLL MMMLMMMLK ... AC AB AA M L K ... C B A
Note this dc command requires tail recursion to work. This works on dc version 1.3.95 (Ubuntu 12.04) but not 1.3 (OSX Mavericks).
APL (59)
↑Z/⍨{~∨/,↑⍷∘⍵ ̈4/ ̈⎕A[⍳13]} ̈Z←⊃,/{↓⍉⎕A[1+(⍵/13)⊤ ̄1⌽⍳13*⍵]} ̈⍳9
Written in its own alphabet :)
It's a bit long. It also takes a long time to run with 9, try it with a lower number to test if you want.
Explanation:
{...} ̈⍳9: for each number⍵from 1 to 9:⍳13*⍵: get all numbers from 1 to13^⍵̄1⌽: rotate the list to the left by 1 (so we have13^⍵,1,2, ...,13^⍵-1, which turns into0, 1, 2 ...modulo13^⍵).(⍵/13)⊤: encode each number in base 13 using⍵digits⎕A[1+...]: add one (arrays are 1-indexed) and look up in⎕A(the alphabet)↓⍉: turn the matrix into a vector of vectors along the columns.
Z←⊃,/: join each inner vector of vectors together, giving us a list of possible names (but it doesn't meet the rules yet).{...} ̈: for each name, test if it meets the 4-repeated-chars rule:4/ ̈⎕A[⍳13]: for each character, generate a string of 4 of that character⍷∘⍵ ̈: for each string, test if it is present in⍵∨/,↑: take the logical or of all these tests,~: and invert it, so that1means that it meets the rules and0means it doesn't.
Z/⍨: select fromZall the elements that meet the ruels↑: display each one on a separate line
-
9\$\begingroup\$ I'm disappointed. Given its reputation, you'd think APL would have a one-character solution for this, that no keyboard could type. \$\endgroup\$Mark– Mark2014年06月01日 05:32:29 +00:00Commented Jun 1, 2014 at 5:32
-
7\$\begingroup\$ @Mark, I am certain that APL does have that, but no one knows what the character is :) \$\endgroup\$Paul Draper– Paul Draper2014年06月01日 06:49:35 +00:00Commented Jun 1, 2014 at 6:49
-
1\$\begingroup\$ one should write this onto a stone, and when future humans find this, they might just think it's just primitive written language. \$\endgroup\$CincauHangus– CincauHangus2014年06月02日 09:51:56 +00:00Commented Jun 2, 2014 at 9:51
Perl, (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 66 (削除ここまで) 50 characters
$"=",";map/(.)1円{3}/||say,glob$i.="{@a}"for@a=A..M
Usage:
$ perl -E 'code' > output_file
The nice thing is that the prints are buffered, so you get all 1-character solutions printed first, followed by 2-character words and so on.
-
\$\begingroup\$ The best thing about this solution is the boob to the left of the 1. \$\endgroup\$Dan Hanly– Dan Hanly2014年06月28日 13:02:08 +00:00Commented Jun 28, 2014 at 13:02
Perl - 35 bytes
#!perl -l
/(.)1円{3}|[N-Z]/||print for A..1x9
Counting the shebang as one byte.
This is a loose translation of histocrat's answer.
A..1x9 is a bit of an oddity; this is shorthand for 'A'..'111111111'. The accumulator will never actually reach the terminal value (it contains only upper-case letters), but it will still terminate once it becomes longer than 9 characters long. This can be tested, for example, by using 1x4 instead.
-
\$\begingroup\$ Respect! Now why didn't I think of that? ;) \$\endgroup\$Zaid– Zaid2014年06月06日 10:07:50 +00:00Commented Jun 6, 2014 at 10:07
-
\$\begingroup\$ Note that Ruby doesn't have to create the entire range in order to iterate it, either. The only reason the code in my comment requires such a huge amount of memory is that it turns the range into an Array (so that it can use
Array#-). \$\endgroup\$Ventero– Ventero2014年06月06日 16:01:29 +00:00Commented Jun 6, 2014 at 16:01 -
\$\begingroup\$ @Ventero Ahh yes,
grepwill do that. I'm not entirely fluent in Ruby. \$\endgroup\$primo– primo2014年06月06日 16:40:06 +00:00Commented Jun 6, 2014 at 16:40
Pyg (Waaay too long, for a language made for golfing)
whispers: 101...
Pe(*ItCh(((J(x)for x in ItPr("ABCDEFGHIJKLM",repeat=j)if not An((i*3 in x)for i in x))for j in R(14))))
Even though this is close to how I would actually do it in Python:
from itertools import *
for i in range(14):
for j in ("".join(k) for k in product("ABCDEFGHIJKLM",repeat=i) if not any((i*3 in k) for i in k)):
print j
Minus the long line complication of course ;)
Pyth, 34 characters
Kf<T"n"GJKFbJI>lb9Bb~Jm+bdfXVb*Y3K
Explanation:
Kf<T"n"G K = list of letters in the alphabet before n.
JK J = copy of K
FbJ For b in J:
I>lb9B If length of b > 9: break
b print(b)
~J J+=
~Jm+bd J+=map(lambda d:b+d,
XVb*Y3 index of Y*3 in reversed(b)
fXVb*Y3K filter for non-zero for Y in K on function index of Y*3 in reversed(b)
~Jm+bdfXVb*Y3K J+=map(lambda d:b+d, filter(lambda Y:index of Y*3 in reversed(b), K))
Python 2 - 212 bytes
from itertools import chain,product as p
a='ABCDEFGHIJKLM'
q={c*4 for c in a}
c=0
for n in chain(*(p(*([a]*l)) for l in range(1,10))):
n=''.join(n)
if not any(u in n for u in q):print n
c+=1
if c==10**9:break
Japt, 21 bytes
Ep9 osE kè/0|(.)1円{3}
Try it online! (the link only computes up to 14**4.)
How it works
Ep9 osE kè/0|(.)1円{3}/
Ep9 14**9
osE Range from 0 to n (exclusive), mapped through base-14 conversion
kè Remove elements that match at least once...
/0|(.)1円{3}/ the regex that matches a zero or four times same char.
Assumes a standard ECMAScript 2017 implementation as the JS layer (and enough memory to store the array), where an Array object can have maximum of 2**53-1 length.
f(k) = k^9 + k^8 + k^7 - 5*k^6 + k^5 + k^4 + 4*k^3 - 2*k^2 + k. Sage implementation: goo.gl/0srwhq \$\endgroup\$105.8GBall said and done! I'm glad the stars didn't go out... or maybe you have to print the list for that to happen...? \$\endgroup\$