4
\$\begingroup\$

192 is such a number, together with its double (384) and triple (576) they contain each 1-9 digit exactly once. Find all the numbers have this property.

No input.

Output:

192 384 576
219 438 657
273 546 819
327 654 981
\$\endgroup\$
  • 4
    \$\begingroup\$ The problem with golfing this kind of problems is that they don't need a real program. Just printing 192 219 273 327 would do. \$\endgroup\$ – Dr. belisarius Mar 28 '13 at 6:23
  • \$\begingroup\$ 192384576*3 = 577153728 which contains some digits twice and others not at all. \$\endgroup\$ – Peter Taylor Mar 28 '13 at 7:42
  • 2
    \$\begingroup\$ I think you should rephrase to question to something like: 192 is such a number; together with its double (384) and triple (576) they contain each 1-9 digit exactly once.. The current form is a bit misleading \$\endgroup\$ – Cristian Lupascu Mar 28 '13 at 8:10
4
\$\begingroup\$

GolfScript (31 30 chars)

1246`{[27*1131-..2*\3*]' '*n}/

(thanks to Howard for 31->30); or for a non-hard-coded approach, 33 32 (thanks again to Howard) chars:

333,{[..2*\3*]' '*}%{.&0-,9>},n*

If the output doesn't have to be character-for-character identical to the text in the question (i.e. if that is a sample), we can shorten to 27 chars:

[4.)7 9]{[27*84+..2*\3*]}%`
\$\endgroup\$
  • \$\begingroup\$ You may save a char and omit the back tick \$\endgroup\$ – Howard Mar 28 '13 at 9:33
  • \$\begingroup\$ And even shorten it to 31: 333,{[..2*\3*n]' '*..&0-,10>*}/. \$\endgroup\$ – Howard Mar 28 '13 at 9:40
  • \$\begingroup\$ @Howard, my current understanding of the question is that it's a Kolmogorov-complexity one: reproduce the exact string. Your 31-char solution would add trailing spaces to the lines. If we remove the exact string restriction then there are certainly places that savings can be made. \$\endgroup\$ – Peter Taylor Mar 28 '13 at 9:44
  • \$\begingroup\$ I didn't read that in the specification - only to somehow have the correct output format. You may be right, but the formulation simply doesn't read reproduce the exact string to me. \$\endgroup\$ – Howard Mar 28 '13 at 9:49
  • \$\begingroup\$ Following your argument you must recall one of your first two solutions: one has a trailing newline and one hasn't ;-) \$\endgroup\$ – Howard Mar 28 '13 at 9:58
6
\$\begingroup\$

APL (20)

192 219 273 327∘.×⍳3

Just prints the numbers multiplied by 1, 2, and 3.

Without hardcoding (25)

{∧/1↓⎕D∊⍕k←⍵×⍳3:⎕←k}¨⍳400
\$\endgroup\$
  • \$\begingroup\$ I don't know APL... can you take advantage of the fact that 192 and 273 can be rotated 1 to the right to obtain 219 and 327 respectively? \$\endgroup\$ – boothby Mar 30 '13 at 4:34
  • \$\begingroup\$ odd... that holds for all numbers of both sequences \$\endgroup\$ – boothby Mar 30 '13 at 4:41
  • \$\begingroup\$ @boothby: You're thinking like an APL programmer, but unfortunately, in this case that would be longer than just writing them out, especially since the rotation itself isn't constant. \$\endgroup\$ – marinus Apr 2 '13 at 8:27
  • \$\begingroup\$ What do you mean the rotation isn't constant? \$\endgroup\$ – boothby Apr 2 '13 at 17:05
2
\$\begingroup\$

GolfScript 44 39

Here's a program that's slightly shorter than the trivial (just print the numbers) solution:

333,{4,(;{1$*}%\;.''*$10,0-''*={.p}*;}/

(thanks Howard & Peter)

v1:

333,{3,{)}%{1$*}%\;.''*$9,{)}%''*={p}{;}if}/

Online test here.

\$\endgroup\$
  • \$\begingroup\$ In your case you can shorten {p}{;}if into {.p}*;. \$\endgroup\$ – Howard Mar 28 '13 at 9:43
  • \$\begingroup\$ And two others: 3,{)}% -> 4,0- and same with 9,{)}%. \$\endgroup\$ – Howard Mar 28 '13 at 9:52
  • \$\begingroup\$ Or 4,(;, which would be more useful if the token following were a positive integer. \$\endgroup\$ – Peter Taylor Mar 28 '13 at 9:55
  • \$\begingroup\$ Or of course 4,1> which fulfills the same. \$\endgroup\$ – Howard Mar 28 '13 at 10:02
2
\$\begingroup\$

Python: 84 77 73 characters

for i in range(328):
 if`set(`i`+`i*2`+`i*3`)-{'0'}`[45:]:print i,i*2,i*3
192 384 576
219 438 657
273 546 819
327 654 981
\$\endgroup\$
  • \$\begingroup\$ That's some fine golfing, my friend. I do not understand how python parses those backticks... \$\endgroup\$ – boothby Mar 30 '13 at 4:52
  • \$\begingroup\$ @boothby Nobody ever uses them since they were a bad idea in the first place but all they do is call repr(). It's the shortest way i could think of to concatenate the numbers \$\endgroup\$ – jamylak Mar 30 '13 at 6:13
  • 2
    \$\begingroup\$ Oh, backticks are a good idea and quite familiar. But... the way you've nested them is just evil. \$\endgroup\$ – boothby Mar 30 '13 at 6:21
2
\$\begingroup\$

Haskell: 55 characters

[(i,2*i,3*i)|i<-[0..333],9==length(nub$show$1002003*i)]

Or 65 characters if it is to be compiled and not just interpreted:

main=print[(i,2*i,3*i)|i<-[0..333],9==length(nub$show$1002003*i)]
\$\endgroup\$
  • 1
    \$\begingroup\$ Save 1 by moving the parenthesis to length(nub$show$1002003*i). Also, the use of $ to apply print is redundant. \$\endgroup\$ – hammar Apr 6 '13 at 18:10
1
\$\begingroup\$

Ruby: 76 65 60 characters

(99..999).map{|n|s=[n,2*n,3*n]*" ";s=~/(\d).*\1|0/||puts(s)}

Sample run:

bash-4.2$ ruby -e '(99..999).map{|n|s=[n,2*n,3*n]*" ";s=~/(\d).*\1|0/||puts(s)}'
192 384 576
219 438 657
273 546 819
327 654 981
\$\endgroup\$
  • 1
    \$\begingroup\$ In this case $_=[n,2*n,3*n]*" " is much shorter. \$\endgroup\$ – Howard Mar 28 '13 at 18:11
  • 1
    \$\begingroup\$ Another one: put the no-zero part into the first regex: /(\d).*\1|0/. \$\endgroup\$ – Howard Mar 29 '13 at 7:41
0
\$\begingroup\$

C#, 109 characters

String.Format("{0}{1}{2}",a,a*2,a*3).Length ==9 && String.String.Format("{0}{1}{2}",a,a*2,a*3).Distinct()==9;
\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf, @ZafarYousafi. Please note that code golf is a challenge of writing the shortest code possible. That means, here we write unindented and almost unreadable code and force the limits of the language syntax to shorten our codes as possible. \$\endgroup\$ – manatwork Apr 1 '13 at 11:59

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.