121
\$\begingroup\$

Your favourite programming language has just had a birthday. Be nice and sing it the Happy Birthday song.

Of course you should accomplish this by writing a program in that language. The program takes no input, and writes the following text to the standard output or an arbitrary file:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear [your favourite programming language]
Happy Birthday to You

You should substitute the bracketed part (and omit the brackets).

This is a code golf — shortest code wins.

UPDATE

I'm glad that the question aroused great interest. Let me add some extra info about scoring. As stated originally, this question is a code golf, so the shortest code is going to win. The winner will be picked at the end of this week (19th October).

However, I'm also rewarding other witty submissions with up-votes (and I encourage everybody to do so as well). Therefore although this is a code-golf contest, not-so-short answers are also welcome.

Results

Congratulations to Optimizer, the winner of this contest with his 42 byte long, CJam submission.

Leaderboard

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

/* Configuration */

var QUESTION_ID = 39752; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 48934; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

function answersUrl(index) {
  return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

var OVERRIDE_REG = /^Override\s*header:\s*/i;

function getAuthorName(a) {
  return a.owner.display_name;
}

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });

  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    var lang = a.language;
    if (/<a/.test(lang)) lang = jQuery(lang).text();
    
    languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
  });

  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);

  langs.sort(function (a, b) {
    if (a.lang > b.lang) return 1;
    if (a.lang < b.lang) return -1;
    return 0;
  });

  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<div id="language-list">
  <h2>Winners by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
17
  • 2
    \$\begingroup\$ Can we count the name of the programming language as one byte because it would only seem fair to people coding in a long-winded language such as JavaScript vs. someone doing it in C. We are really looking for most creative logic right? \$\endgroup\$ – MonkeyZeus Oct 13 '14 at 18:49
  • 14
    \$\begingroup\$ Why all the upvotes for this despite all the downvotes for the Bonbon song? This is just as boring a challenge. \$\endgroup\$ – xnor Oct 14 '14 at 7:15
  • 10
    \$\begingroup\$ I should probably mention that the song is copyrighted and distributing these programs may cause a DMCA \$\endgroup\$ – ratchet freak Oct 14 '14 at 14:38
  • 12
    \$\begingroup\$ guys! Happy Birthday song is now in public domain!! \$\endgroup\$ – Optimizer Sep 23 '15 at 10:01
  • 5
    \$\begingroup\$ At least one of the answers prints a leading newline. Is that allowed? \$\endgroup\$ – Dennis Jun 19 '16 at 21:32

195 Answers 195

1
3 4 5 6
7
0
\$\begingroup\$

Charcoal, 56 bytes

≔Happy Birthdayη≔ to YouτP⁺ητ↓P⁺ητ↓P⁺η”↶ G↗5▶h…≡-⌕”↓P⁺ητ

Try it online! (Link to the verbose version.)

It wasn't fair that Charcoal still hadn't its own birthday party...

\$\endgroup\$
1
  • \$\begingroup\$ 47 \$\endgroup\$ – ASCII-only Aug 6 '18 at 6:23
0
\$\begingroup\$

Noether, 54 bytes

"Happy Birthday "~s"to You"+~mP?P?sP"Dear Noether"P?mP

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MBASIC, 93 bytes

1 FOR I=1 TO 4:PRINT"Happy Birthday ";:IF I=3 THEN PRINT"Dear MBASIC"ELSE PRINT"to You"
2 NEXT
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 45 bytes

"Happy Birthday to You"·E<"Dear MathGolf"+\]n

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ wow this challenge is 4 years old now \$\endgroup\$ – qazwsx Oct 6 '18 at 1:43
  • \$\begingroup\$ I was surprised that there weren't any answers from some newer golfing languages, but I like these simple challenges. With some string compression this answer could be shorter, but I haven't implemented that yet. \$\endgroup\$ – maxb Oct 6 '18 at 7:02
0
\$\begingroup\$

A0A0 - 309 bytes

A0A0
A0C3G1G1G1G1G1G1G1G1G1G1G1G1G1G1G1A0
A0P72P97P112P112P121P32P66P105P114P116P104P100P97P121P32G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3A0
G-3
G1G4G7G4
A0A0
A0C3G1G1G1G1G1G1G1A0
A0P116P111P32P89P111P117P10G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3A0
G-3
G-8G-8
P68P65
P101P48
P97P65
P114P48
P32P10
G-5G-14

Explanation:

A0A0
A0C3G1G1G1G1G1G1G1G1G1G1G1G1G1G1G1A0
A0P72P97P112P112P121P32P66P105P114P116P104P100P97P121P32G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3A0
G-3

Creates a loop which prints "Happy Birthday " and jumps every iteration.

G1G4G7G4

The positions the code should jump to each iteration.

A0A0
A0C3G1G1G1G1G1G1G1A0
A0P116P111P32P89P111P117P10G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3A0
G-3

Creates a loop which will print "to you" and jumps out of the loop after each iteration.

G-8G-8

More positions to jump to, but this time for the second loop (also acts as a sort of counter, adding an additional G-8 would print "Happy Birthday " one more time).

P68P65
P101P48
P97P65
P114P48
P32P10
G-5G-14

Prints "Dear A0A0" and jumps back to the first loop.

Test it here.

\$\endgroup\$
0
\$\begingroup\$

Cubically, 251 bytes

⇒:4+@:5+2/1+55@+2S-0@@S'+1@f2/1+52@F+5@F'+1@:4/1+6255@U-1@U'/6+1*@S-0@S'+1/1*@f2
⇒:5/1+3@
⇒/6+6366@/4+552@f2/67+55@/3+552@B+1@B'/6+1@
f1f3f1f3f1//3+6635@/3+551@:5+2/1+55@/3+6552@f2+/3+165@-6+355@/6++++55@/3+6551@:1+55@:5+2/1+55@:2+55@@/6+61*@/6+1@f1f3

Try it online!

Explanation:

First, second and third lines are functions.
First line prints "Happy Birthday " (note the trailing space)
Second line prints a space (to golf it down)
Third line prints "to You\n"

Fourth line is the program. It simply calls functions and prints "Dear Cubically\n" on the third line.

\$\endgroup\$
0
\$\begingroup\$

Gol><>, 55 bytes

a"Happy birthday to you"3F6sK|6sR}a"Dear Gol><>"6sR{r~H

Voila, explanation coming soon!

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stax, 33 bytes

Çÿ╣á╕♠½ß¡U¶[k╔╒j⌂?6▓╓○÷⌂I╬&⌂Γ╚?╟A

Run and debug it

\$\endgroup\$
0
\$\begingroup\$

GolfScript, 49 bytes

Just a blatant port of the CJam answer.

"Happy Birthday to You
"...15<"Dear GolfScript
"@

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ A month earlier and this would have been a proper salutation to Golfscript \$\endgroup\$ – Lyxal Jan 22 '20 at 9:38
0
\$\begingroup\$

Wren, 78 bytes

I have forgotten about Wren so I'm trying to use it.

Fn.new{(1..4).map{|i|"Happy Birthday "+(i==3?"Dear Wren":"to You")}.join("
")}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Scala, 59 bytes

print("Happy Birthday to You\n"*4 patch(59,"dear Scala",6))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 39 bytes

”ŽØ¢© ”“€„ “«”€î”«U,,”ŽØ¢©¬Ž 05AB1E”,X,

Try it online!

”ŽØ¢© ”“€„ “«”€î”«  # push "Happy Birthday to You"
U                   # store top of stack in variable X
,,                  # output top of stack twice
”ŽØ¢©¬Ž 05AB1E”     # push "Happy Birthday Dear 05AB1E"
,                   # output top of stack
X,                  # output X
\$\endgroup\$
0
\$\begingroup\$

Sandwich, 64 bytes

lhHappy Birthday
lt$h to you
ld$h dear sandwich

p$t
p$t
p$d
p$t

Bit of a naive approach, but it works.

Explanation: Sandwich is comprised of opcodes. The first letter of each line is the opcode, and the rest of the characters of the line are the arguments that are passed to the opcode.

Above code with comments:

lhHappy Birthday # Create variable h and assign "Happy Birthday"
lt$h to you # Create variable t and assign "$h to you", which resolves to Happy Birthday to you
ld$h dear sandwich # Create variable d and assign "$h dear sandwich", which resolves to Happy Birthday dear sandwich

p$t # print out variable t
p$t
p$d # print out variable d 
p$t
New contributor
Y45HK4R4ND1K4R is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$ – Makonede Apr 15 at 15:50
0
\$\begingroup\$

Branch, 145 bytes

72/97Q/112/;/121N/32O/66/105/114P/116R/104T/100/q/n/oa[./]a\Br/111S/o/89/s/117/10b[./]a[./]b[./]a[./]b\68.101.q.p.o.66.p.q.110.99.t.10.a[./]b[./]

Try it on the online Branch interpreter!

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

C# LinqPad, 103 bytes

Regex.Replace(String.Join("\n",Enumerable.Repeat("Happy Birthday to You",4)),"^((.*\n){2}.*)to.+","$1C#")
\$\endgroup\$
1
3 4 5 6
7

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.