Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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, see http://en.wikipedia.org/wiki/Happy_number for details).

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!

share|improve this question
    
What prevents one from doing /*program1*/program2 and then program1/*program2*/? I think you should disallow comments. –  William Barbosa 5 hours ago
    
@WilliamBarbosa Why? That will hardly be an optimal solution. –  Martin Büttner 5 hours ago
    
But you could also share some parts and not share others which makes it much easier –  proud haskeller 5 hours ago
    
@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. –  Martin Büttner 4 hours ago
    
Can a 60 length program print sixTY instead of sixty (Basically asking if the output should be in all lowercaps or not) ? –  Optimizer 3 hours ago

3 Answers 3

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.

share|improve this answer
    
I'm very busy right now. I'll post an explanation later. –  Dennis 2 hours ago
    
+1 for the 31-byte improvement, and "forTy-nine". –  Josiah Winslow 39 mins ago

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.

share|improve this answer
2  
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. –  Martin Büttner 5 hours ago
    
I see people are not noticing you wrote the question and upvoting you... I totally agree with martin. –  proud haskeller 5 hours ago
    
@proudhaskeller There is absolutely nothing wrong with self-answering. –  Quincunx 4 hours ago
    
I say there is nothing wrong with answering myself or commented code. –  Josiah Winslow 3 hours ago
    
@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. –  Martin Büttner 3 hours ago

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-\?:+";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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