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\$
  • 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
  • 4
    \$\begingroup\$ At least one of the answers prints a leading newline. Is that allowed? \$\endgroup\$ – Dennis Jun 19 '16 at 21:32

185 Answers 185

1 2
3
4 5
7
2
\$\begingroup\$

PHP 67 63 62

echo$z=($s="Happy Birthday").$t=" to You
","$z$s Dear PHP
$z";

EDIT: As @manatwork pointed out, without the second echo I have 4 chars less!!

EDIT2: Other improvement according to @manatwork

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice one. But you not need a separate echo to have the earlier set variables available. Make the 2nd line: ","$z${s}Dear PHP. \$\endgroup\$ – manatwork Oct 14 '14 at 12:30
  • \$\begingroup\$ @manatwork great suggestion! \$\endgroup\$ – Alessandro Oct 14 '14 at 12:34
  • \$\begingroup\$ You can spare 1 more character by getting rid of those braces around variable ${s}. If you add the middle space to the both second half lines instead of the first half line, the length increases by 1 character, but you will be able to remove the braces, sparing 2 characters: pastebin.com/Qw7dWHGs \$\endgroup\$ – manatwork Oct 14 '14 at 12:35
  • \$\begingroup\$ @manatwork I didn't got this... If I add the middle space to both the second half lines (before "to You" and before "Dear PHP") then I'm adding 2 chars and removing the brackets I have the same count \$\endgroup\$ – Alessandro Oct 14 '14 at 12:39
  • \$\begingroup\$ Now: "Happy_" + "You" + "Dear", proposed : "Happy" + "_You" + "_Dear". So actually one of them you move and add just one new. \$\endgroup\$ – manatwork Oct 14 '14 at 12:44
2
\$\begingroup\$

Visual FoxPro - 77

Not really my favorite language, but makes golfing somewhat interesting. Here is my greetings to it:

a="Happy Birthday "
b="to You"
?a+b
?a+b
?a+"Dear "+_screen.Caption
?a+b

This abuses the fact that _screen.Caption (the main window title) initially contains Microsoft Visual FoxPro, handy here. If I'm allowed to call the language by its "friendly name", I can just replace that for "Fox" and save 10 characters.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

PowerShell 62

0..3|%{"Happy Birthday "+("to You","Dear PowerShell")[$_-eq2]}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Delphi, 154 146 139

uses{$APPTYPE CONSOLE}SysUtils,StrUtils;var i:byte;begin
for i:=0to 3do WriteLn('Happy birthday '+IfThen(i=2,'dear Delphi','to you'))end.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Which Delphi version is this? I never met one with such Output parameter. And better choose another integer type, with a shorter name. For example Byte. \$\endgroup\$ – manatwork Oct 16 '14 at 15:17
  • \$\begingroup\$ An insignificant bug: the 'to you' and 'dear Delphi' should be reversed. \$\endgroup\$ – manatwork Oct 16 '14 at 15:27
  • 1
    \$\begingroup\$ Some minor shortening which work in FreePascal: move the directive between uses and the first unit name to be able to remove the space (1 char); remove unnecessary newline characters (3 chars); remove the ; before end (1 char); remove the space characters between numeric literals and the following keywords (2 chars). See whether any of those works in your Delphi too. \$\endgroup\$ – manatwork Oct 16 '14 at 15:34
  • \$\begingroup\$ @manatwork, thank you for your suggestions, it's Delphi XE2. \$\endgroup\$ – R-D Oct 17 '14 at 7:19
  • 1
    \$\begingroup\$ There's no need for Output,. \$\endgroup\$ – TLama Oct 20 '14 at 23:08
2
\$\begingroup\$

TeX - 70

\def\h{Happy Birthday }\def\y{to You}\h\y

\h\y

\h Dear TeX

\h\y\bye

Note that the line breaks are intentional and actually required.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

LaTeX, 182

This isn't intended to be a serious competitive entry, but just to do something fun. Therefore, disregard the char count. I don't need any golfing tips for this, because it is intentionally ungolfed.

\documentclass{article}
\newcommand{\h}[1]{Happy Birthday to {#1}}
\newcommand{\g}{\h{You}}
\begin{document}
\begin{verse}
\g \\
\g \\
\h{\LaTeX} \\
\g \\
\end{verse}
\end{document}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C (74 characters)

main(i){for(i=4;i;)printf("Happy birthday %s\n",i---2?"to You":"Dear C");}

Python 2 (72 characters)

for k in range(4):print"Happy birthday",k==2 and"Dear Python"or"to You"

Python 3 would need parentheses around the printed string, so two character more, so 74.

PHP (66 characters)

for($i=4;$i;)echo"Happy birthday ".($i---2?"to You\n":"Dear PHP\n");
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In C, i---2 saves a character. Also {} after for isn't needed. \$\endgroup\$ – ugoren Oct 20 '14 at 9:32
2
\$\begingroup\$

Cardinal, 113 107 88 82 characters

I fixed the errors in the original interpreter, so the operators work as specified. This enabled me to shrink down the code by another 19 25 characters:

4th version:

v8888#;++"lanidraC raeD " <
8%+++>>"Happy Birthday"-jv^
>8888@^        ;"uoY oT "<

Output, using my interpreter fixes:

Executing program..

Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear Cardinal
Happy Birthday To You

Execution complete.
>

Second version, saving 6 chars: I changed the o’s and O’s to v^<> because the available interpreter has some errors with the handling of the rotation operators o and O, j and J and some other errors. One has to use the wrong rotation operators to make it work, but I prefer using a version that works according to specification. The length of the code stays the same. This version works properly with the interpreter provided at the link given at the bottom of my comment: (107 characters)

%->"Happy Birthd"v
x ^D+;"uoY oT ya"<
   >"Happy Birthday D"v
v"ppaH";"lanidraC rae"<
>"y Birthday To You"@

Output, using the "original" faulty interpreter:

Executing program..

Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear Cardinal
Happy Birthday To You

Execution complete.
>

You can test the program if you save it as txt file and drag it onto the interpreter exe for Windows.

http://esolangs.org/wiki/Cardinal

Here is the interpreter: http://floatation.webs.com/Cardinal.zip

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

CMD - 84 Bytes

cmd/v/c"set 1=to You&set 2=Dear CMD&for %a in (1,1,2,1)do @echo Happy Birthday !%a!"
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

jq: 54 52 characters

(49 characters code + 3 characters command line option.)

0,0,1,0|"Happy Birthday "+["to You","Dear jq"][.]

Sample run:

bash-4.3$ jq -n -r '0,0,1,0|"Happy Birthday "+["to You","Dear jq"][.]'
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear jq
Happy Birthday to You

On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Vim, 90 45 bytes

i
Happy Birthday to you
[ESC]
yypppPWWd$adear Vim

90 chars version:

i
Happy Birthday to you dear Vim
[ESC]
:%s/\(.* .* \)(.. .*\) \(.* .*\)/\1\2\r\1\2\r\1\3\r\1\2

I'm certain there is a quicker way but this is what I just came up with :)

Puts out

Happy Birthday to you
Happy Birthday to you
Happy Birthday dear Vim
Happy Birthday to you
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Crayon, 48 46 bytes (non-competing)

"Happy Birthday to You
"4*q2Y15X"Dear¤Crayon"q

Try it online! Non-competing because Crayon is much newer than this challenge.

Explanation

Crayon's output is built on a 2-dimensional "canvas", which makes it ideal for ASCII art challenges. Text is drawn with a "crayon", which is a sort of cursor that moves around the canvas. Here's how this program works:

"Happy Birthday to You
"                        Push this string to the stack.
 4*                      Repeat the string 4 times.
   q                     Draw the string at the crayon (at (0,0) by default).
    2Y15X                Move the crayon to the "to" in the third line.
         "Dear¤Crayon"q  Push this string and draw it at the crayon.
                         Implicit: Output canvas

The ¤ represents byte \x1F; Crayon does not distinguish between the two. By default, a space does not overwrite the character underneath, so ¤ exists as a sort of "overwriting space".

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Vim, 40 36 bytes

4OHappy Birthday to You<esc>k2bCDear Vim

4 bytes saved thanks to @DJMcMayhem by using O instead of i...\n and k2bC instead of kd2bC

This uses a completely different approach to the previous Vim answer and hence I posted it as a separate answer. Outputs a trailing newline, but can be removed at the cost of one extra byte by using Jk instead of k.

Since V is reverse compatible with V, you can Try it online!

Take that CJam!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This is the shortest competing solution, as far as I can tell. \$\endgroup\$ – primo Mar 12 '18 at 14:15
2
\$\begingroup\$

IDL (Interactive Data Language) – 47

Late entry (my first time here), but this looks like 3rd place after CJam and J!

Using IDL's Implied Print feature (since version 8.3), typing this at the command prompt:

'Happy Birthday '+[(u='to You'),u,'Dear IDL',u]

… will print the desired result to the console:

IDL> 'Happy Birthday '+[(u='to You'),u,'Dear IDL',u]
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear IDL
Happy Birthday to You

It works by creating (and implicitly printing) a four-element array, with 'Happy Birthday ' appended to each of the four desired endings, made more concise/cryptic by assigning a variable u the value 'to You' in the first instance, and reusing it twice later!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 52 bytes

"Happy Birthday to You"k₆,"Dear Brachylog"g,?,?↺,?ẉᵐ

Try it online!

-1 thanks to Fatalize.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can do 52 bytes by replacing Z with ? and removing the first Z that unifies with the string. \$\endgroup\$ – Fatalize Aug 2 '17 at 14:29
  • \$\begingroup\$ @Fatalize Oh right the string unifies with ?. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 17:47
2
\$\begingroup\$

Unary, 2.715E2345 bytes

A string of zeroes with a length of 271544303753521374345784023617462447448766796991634195623271771343788064494033932467809637777433783261069835136468316635541569006957484008922172956907493623220358279584621153727394685316177875072717611870164011317998138133192412590581118711745080372132354520144049887264020357596958970682508119612069072184789433001139110110044141818883356102010044766108874743534376722033752443907309500308124800880956659496453762860029730933467190897780852535562502370486347488149358821225254933647032473098015846900520720300239993510123716590092279978630211422199772105659685365124211730629429856761187973769267382198582572968422104301126603859498501185917608874757741610893482041977003001381483481251619529628722427408338582339527157335234122942381275923949390605570564504822082772136057434464671123507495503027121245882553216512907124492451345153924551954324886539679044057026572440371334484657248811556813671966134409423451672608237773610848374597757496223902091127753821502096156978576733667670645804932685651998882744625271805968408192138003017383834286411465787825657955474390895833048578431020834761999769245058175985114327604667460675218915598066545717356728963407131382225834981689192053204022543453105950548359569307644293248265610511261123308873236361708287888512920927207140733264710153499926338085521461180344119631268486929440101264937117199882754841067458895376078972797675755102211602033168204711199744629239139853151734765325982327999734282103666604630769384354544377780974557091020653916122475973344426958326542939871981389127350485205414017350058299535652163085166091203298279054857159449355550278205308529995426116266317622349698148579685752377806473014344962213328458584663395562552762790574493037199848761427965675120816268240569132735553272845975553355819867289784427736974879735152861965319683108949477353673024614920892135291073944764379263817004591234093204928220689300382675729272978243082941205866127086587758323575891345926945843642967213885479373059430434109978135813970824413339027287055293233734939242557385167820253321966087885920624040254325137166864535273452145628538868866835395916784107310616214635120965693431403440614294637759561044455445706647271846433473546725069631283599186867863795122114084809385455487348989575611753135663244291563170055464760084089978670081473032469603515349502029160880861825869931824089974215244

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ I thought this was a good score till I saw the "E" \$\endgroup\$ – NoOneIsHere Sep 23 '17 at 20:45
2
\$\begingroup\$

Aceto, 57+1 for -l flag = 58 bytes

"Happy Birthday "ddd"to you"ddQJpnQJpn"Dear Aceto"QJpnsJp

Try it online!

"Happy Birthday "                           push that string
                 ddd                        duplicate three times
                    "to you"                   push that string
                            dd                    duplicate twice
                              Q                    Pull from bottom of stack
                               Jpn                 Join, print, & newline
                                  QJpn               pull from bottom, &Join,print&\n
                                      "Dear Aceto"        push that string
                                                  QJpn      Pull,Join,print,\n
                                                      sJp     swap, Join, and print
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Stax, 30 bytes

ï■σ∞úφ♂mê▌δ↓T┌C▒7w↓√éº┌│╦╫C∩█ï

Run and debug online!

Explanation

Uses ASCII representation to explain:

`=%5#`GG`!0 ZrO"`GdG}`I#aq5J^oD}(`pQ

                    }                   Define a subroutine after the bracket
                     `I#aq5J^oD}(`      Push "Happy Birthday "
                                  p     Pop the string and print without newline
                                   Q    Peek the stack and print with newline

`=%5#`                                  Push "to You"
      GG                                Call the subroutine twice
        `!0 ZrO"`                       Push "Dear Stax"
                 G                      Call the subroutine
                  d                     Pop and discard "Dear Stax"
                   G                    Call the subroutine again
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I like your favorite language. Here's 28 for the lead. \$\endgroup\$ – recursive Mar 16 '18 at 21:12
2
\$\begingroup\$

R, 56 bytes

cat("",u<-"to You
",u,"Dear R
",u,sep="Happy Birthday ")

Try it online!

The old sep= trick.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C (gcc) and aplay, 360, 175, 167, 151 bytes

Generates the contents of a WAV file onto stdout, singing the melody of Happy Birthday. The output can then be piped to, for example, aplay to listen to it.

Including -lm causes a +4 to score.

Also, thanks to ceilingcat for golfing a couple dozen bytes.

c,i;main(t,T){for(;i<25;i++)for(t=T="$$(((0$$(((0$$((((($$(((0"[i]-32<<9;t--;)putchar(c=sin(exp("!!#!&%!!#!(&!!-*&%#++*&(&"[i]/17.-2)*t)*9*t/T+9);}

Try it online! (won't produce sound, duh...)

Now available on Clyp for listening. Amplified for convenience, may be loud and/or pop on some devices.

Try it offline!

Compile and listen with gcc -w src.c -lm && ./a.out | aplay

Degolf

c,i;
main(t,T) {
    for(;i<25;i++) // 25 notes of the song in the loop, 25 notes of the song
        for(t=T="$$(((0$$(((0$$((((($$(((0"[i]-32<<9; // Select number of samples
                t--;)
            // See below, this has been mutilated quite a bit to golf it.
            // Assign to c for implicit cast to int.
            // A small shortcut is made by only dampening the sine-part of the wave.
            putchar(c=sin(exp("!!#!&%!!#!(&!!-*&%#++*&(&"[i]/17.-2)*t)*9*t/T+9);
}

A sine wave is defined as \$s(t)=A\cdot\sin 2\pi t\$. A sine wave with frequency \$f\$ can thus be expressed as \$s(ft)\$. Now, in this case \$\{t\in\mathbb N \mid0\leq t \leq T \}\$ and \$T:=n\cdot2^{11}\$, so we have to divide \$t\$ with 2048, in order to make the signal function work. Our function is now \$s(2^{-11}\cdot ft)\$.

Since listing the frequencies would take a significant amount of bytes, I have encoded the frequencies as halfsteps relative to A4, represented by 40 or ( in the string. The frequency is thus obtained \$f=440Hz \cdot 2^{c-40\over12}\$. Finally, putting it all together, adding in some dampening and ensuring outputs greater than zero, we get:

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({880 \pi t \cdot 2^{c-40\over12}\cdot2^{-11}} \Big)\Bigg) $$

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({880 \pi t \cdot \exp\Big({{c\ln 2-40\ln 2\over12} - 11 \ln 2}}\Big) \Big)\Bigg) $$

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({ t \cdot \exp\Big({{c\ln 2-40\ln 2\over12} - 11 \ln 2 + \ln 880 \pi}}\Big) \Big)\Bigg) $$

$$ f(t)\approx \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({t \cdot \exp \Big({c\over17}-2\Big)} \Big)\Bigg) $$

And finally, we select \${A\over2}=9\$ to conserve bytes.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Japt, 41 38 bytes

`Happy Bir»y  Y
`
³h58Ui` De Japt

Test it

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

PowerShell 79

$a = 'Happy Birthday '
$b = 'to You'
"$a$b`n$a$b`n$($a)Dear PowerShell`n$a$b"
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Clojure : 87 chars

Golfed:

(let[h"Happy Birthday "s(str h"to You\n")t(str h"Dear Clojure\n")](print(str s s t s)))

Ungolfed:

(let [ h "Happy Birthday "
       s (str h "to You\n")
       t (str h "Dear Clojure\n") ]
(print (str s s t s)))
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Or 81 with the similar (let[h"Happy Birthday "s"to You\n"t"Dear Clojure\n"](map #(print h %)[s s t s])) \$\endgroup\$ – tjb1982 Oct 16 '14 at 13:53
1
\$\begingroup\$

C: 87

#define p(a)printf("Happy birthday %s\n",a?a:"to you");
main(){p(0)p(0)p("dear C")p(0)}

Demo

Compiles fine, but since it doesn't return, it might not exit correctly. If you want it to return 0, it'll cost another seven characters:

#define p(a)printf("Happy birthday %s\n",a?a:"to you");
main(){p(0)p(0)p("dear C")return!p(0)}

Demo

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Burlesque - 55 Characters

"Happy Birthday to You"4.*g_15.+"Dear Burlesque".+2iauN

Test it here.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "Happy Birthday " "to You"4.*"Dear Burlesque"3ia?+uN would be a shorter version. \$\endgroup\$ – mroman Sep 25 '15 at 11:45
1
\$\begingroup\$

Python 85

[print('Happy Birthday %s'%('dear Python' if x==2 else 'to you')) for x in range(4)]
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ The if/else idiom can be generally shortened to a list selection: ['to you','dear Python'][x==2]. Instead of string formatting %, you can just use string concatenation. Take a look at some Python golf tips: codegolf.stackexchange.com/questions/54/… \$\endgroup\$ – xnor Oct 14 '14 at 7:05
1
\$\begingroup\$

Cobra - 78

class P
    def main
        print (t=(l='Happy Birthday ')+'to You\n')+t+l+'Dear Cobra\n'+t
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Scala 89 (also it is not my favourite)

(1 to 4).map{i=>"Happy Birthday "+(if(i==3)"dear SCALA" else "to You")}.foreach{println}

Maybe it could be less, it is just a default example, so feel free to improve.

Regarding Java:

If ignoring the excess stuff it is also possible to do a "import static java.lang.System.*;" and then use out.println. So something like this is possible (87 chars):

int i=0;while(i<=3){out.println("Happy Birthday "+((i==2)?"dear Java":"to you"));i++;}

(also it is longer than the solution from Stretch Maniac - see https://codegolf.stackexchange.com/a/39768)

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Befunge: 103

 >"yadhtriB yppaH"67+>:#,_1g:"0"-v
2^_@#-"0"g20p10-1,,,,,,," to You"_1+01p"egnufeB raeD ">:#,_"0"02p#
1
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Railo CFML (79 characters)

for(i=1;i<5;i++){writeoutput("<br>Happy Birthday "&(i<>3?"to you":"dear CF"));}

Just having some fun creating a ridiculous mess:

  variables.endings = ["H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,d,e,a,r, ,C,o,l,d, ,F,u,s,i,o,n,n,n,n,n","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u"];
  variables.iVerse = '';
  for (i = 1; i <= ArrayLen(variables.endings); i++) {
    variables.iLine = '';
    for (i2 = 1; i2 <= ListLen(variables.endings[i]); i2++) {
      iLine = iLine & ListGetAt(variables.endings[i],i2);
    }
    variables.iVerse = variables.iVerse & variables.iLine;
    if (i < ArrayLen(variables.endings)) {
      variables.iVerse = variables.iVerse & "<br />";
    }
  }

  writeOutput(variables.iVerse);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I know it's a little obvious what language this is, but please, describe what language you used and the character count of your code. Also, try to shorten it as much as you can. \$\endgroup\$ – g.carvalho97 Oct 13 '14 at 22:57
  • \$\begingroup\$ @g.carvalho97 I was having too much fun, sorry. I'll fix it. \$\endgroup\$ – Regular Joe Oct 13 '14 at 23:01
1 2
3
4 5
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.