10
\$\begingroup\$

Let's try this again.

The object of this contest is to make two pieces of code that are anagrams of each other (the two must contain the same bytes in a different order), and they will do the following tasks:

  • One must test if an inputted number is happy or prime, and output if the number is either (for example, 7 must output happy prime and 4 must output sad non-prime).

  • The other must output its code size in bytes as a word (a 60-byte program would output sixty, a 39-byte program would output thirty-nine).

If any clarification is needed on the rules, don't hesitate to tell me.

This is a code golf competition, so shortest program wins!

\$\endgroup\$
  • \$\begingroup\$ What prevents one from doing /*program1*/program2 and then program1/*program2*/? I think you should disallow comments. \$\endgroup\$ – William Barbosa Sep 20 '14 at 16:32
  • \$\begingroup\$ @WilliamBarbosa Why? That will hardly be an optimal solution. \$\endgroup\$ – Martin Ender Sep 20 '14 at 16:42
  • \$\begingroup\$ But you could also share some parts and not share others which makes it much easier \$\endgroup\$ – proud haskeller Sep 20 '14 at 16:59
  • 1
    \$\begingroup\$ @proudhaskeller Banning comments doesn't solve that though. You can always stuff characters into strings, variable names or parts of the code that aren't executed for other reasons. \$\endgroup\$ – Martin Ender Sep 20 '14 at 17:40
  • 1
    \$\begingroup\$ FWIW, I don't think you should have deleted your own submission. OP or not, it was an interesting answer. \$\endgroup\$ – Dennis Sep 20 '14 at 20:08
6
\$\begingroup\$

CJam, 80 49 48 characters

UPDATE : Inspired by Dennis' implementation to calculate sum of squares of digits, here is a shorter version

Happy/Sad Prime/Non-prime:

ri:T{Ab2f#:+}G*X="happy""sad"?STmp4*"non-prime">

How it works:

ri:T                                "Read input as integer and store it in T"
    {       }G*                     "Run this code block 16 times"
     Ab                             "Convert the number into base 10"
       2f#                          "Calculate square of each digit"
          :+                        "Sum all the squared digits and put the sum on stack"
X=                                  "Compare the sum after 16th iteration to 1"
  "happy""sad"?                     "If sum is 1, put `happy` to stack, otherwise `sad`"
               ST                   "Put space on stack then put the value of T on stack"
                 mp4*               "Put 4 to stack if input is prime, otherwise 0"
                     "non-prime">   "Put `non-prime` to stack and slice out first four characters if the input number is prime"

forTy-eiGhT

""A"forTy-eiGhT""ri:{b2#:+}*X=appsadSmp4*nnpm>"?

How this works:

""                                  "Push empty string to stack"
  A                                 "Push 10 to stack"
   "forTy-eiGhT"                    "Push `forTy-eiGhT` to stack"
                "ri:....pm>"        "Push this string to stack too"
                            ?       "Keep `forTy-eiGhT` on stack and pop the other string"

Try it online

The first program reads the number from STDIN


My original 80 character solution

Happy/Sad Prime/Non-prime:

r:N{1/~]{i_*T+:T;}/T_s\1=:H!X):XK<&0:T;}g;H"happy""sad"?SNimp"prime"_"non-"\+?:Y

eigHTY

"eigHTY""r:N{1/~]{i_*T+:T}/_s\1=:H!X):XK<&0:T}happysad?SNmp";"prim_";"non-\?:+";
\$\endgroup\$
4
\$\begingroup\$

CJam, 50 49 bytes

Happiness and primality test

li_{Ab2f#:+}30*(T="happy""sad"?S@mp4*"non-prime">

Reads a number from STDIN. Both tests work only for 64-bit integers.

Try it online.

Own length

A"forTy-nine""l_{b2#:+}30*(=happsadS@mp4*pim>"?""

Prints forTy-nine.

\$\endgroup\$
  • \$\begingroup\$ +1 for the 31-byte improvement, and "forTy-nine". \$\endgroup\$ – Josiah Winslow Sep 20 '14 at 21:34
3
\$\begingroup\$

Golfscript - 81

This program tests if a number is happy and/or prime.

~.:a;0.{).a\%!@+\}a*;2="""non-"if"prime"@ {`0\{48-.*+}/}9*("sad ""happy "if@@#get

This program, an anagram of the last, outputs "eighty-one" (its bytesize as a word).

;"eighty-one"#   !""""""""%()***++-..../002489:;=@@@@\\\`aaaaadffiimnppprs{{{}}}~

This should serve as an example.

\$\endgroup\$
  • 2
    \$\begingroup\$ Hm, providing a reference implementation for a code golf challenge in GolfScript might not be the best idea. I believe this one is quite difficult to beat and hence slightly disheartening for participants. \$\endgroup\$ – Martin Ender Sep 20 '14 at 16:31
  • \$\begingroup\$ I see people are not noticing you wrote the question and upvoting you... I totally agree with martin. \$\endgroup\$ – proud haskeller Sep 20 '14 at 17:01
  • \$\begingroup\$ @proudhaskeller There is absolutely nothing wrong with self-answering. \$\endgroup\$ – Justin Sep 20 '14 at 17:29
  • \$\begingroup\$ I say there is nothing wrong with answering myself or commented code. \$\endgroup\$ – Josiah Winslow Sep 20 '14 at 18:16
  • 2
    \$\begingroup\$ @JosiahWinslow There is nothing wrong with it. I'm just saying, you might be missing out on some interesting longer answers if you post a very good solution yourself right away. \$\endgroup\$ – Martin Ender Sep 20 '14 at 18:20
1
\$\begingroup\$

J - 87 char

A naive attempt at this in J. No use of the standard library, though I doubt it would get any shorter by using that.

((sad`happy{~1 e.(,[:+/@:*:,.&.":@{:)^:(1-{:e.}:)^:_);@,' ';'gtv]non-prime'}.~4+4*1&p:)
'((ad`app{~1 .(,[:+/@:*:,.&.":@{:)^:(1-{:.}:)^:_);@, ;onprm}.~4+4*1&p:)']'eighty-seven'
('(ad`app{~1 .(,[:+/@:*:,.&.:@{:)^:(1-{:.}:)^:);@, ;onprm}.~4+4*1&p:']'eighty-seven'"_)

The line on the top is a verb taking an integer and diagnosing its happiness and primality as an output string. The second line is an expression returning the string eighty-seven, while the third is a constant function doing the same. I included both because they were both possible and because I don't know what the ruling will be on function answers as opposed to program ones, and J doesn't have such a thing as no-argument functions—you just give a function a dummy argument.

We lose most of the chars checking for happiness. (,[:+/@:*:,.&.":@{:) is the main body that sums the squares of the digits of a number, and (1-{:e.}:) is the test of whether that number has occurred yet. sad`happy{~1 e. turns this into a word result, and we attach that to the front of the string non-prime, potentially snipping off four characters if the number was actually prime.

In the anagram we just hide all the bits that aren't 'eighty-seven' in a string that we ignore. I could do better if J had more letters to reuse, but it doesn't, so oh well.

\$\endgroup\$
  • \$\begingroup\$ That's a good solution. \$\endgroup\$ – Josiah Winslow Sep 21 '14 at 1:31

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.