60
\$\begingroup\$

Task

Create a program that calculates the factorial of a number using no built-in factorial functions. Easy? The catch is that you must write your entire program (including testing it) in haiku form.

Not enough work
You can use as many haikus as you need, but when pronounced, they must follow the 5-7-5 syllable format.

Scoring

This is a , so you must get the most upvotes to win. Your program must consist of at least one full haiku, and all haikus must be complete.

When reading code, the first line of each haiku will have 5 syllables, the second will have 7, and the third will have 5.

\$\endgroup\$
  • 5
    \$\begingroup\$ Sounds like a perfect fit for something written in Shakespeare: shakespearelang.sourceforge.net/report/shakespeare/… \$\endgroup\$ – Denis de Bernardy Feb 17 '14 at 15:31
  • 2
    \$\begingroup\$ It seems most answers are ignoring "including testing it". \$\endgroup\$ – Anko Feb 17 '14 at 18:16
  • 5
    \$\begingroup\$ I like how you link to a site that correctly says the important thing for Haiku are (a) kiru and (b) a seasonal reference and then only ask for the more or less optional part of counting mora (or syllables in a language that doesn’t really have mora. 😸 \$\endgroup\$ – Christopher Creutzig Feb 17 '14 at 20:40
  • 1
    \$\begingroup\$ I agree with @ChristopherCreutzig -- it would be much more interesting if we had to ensure a seasonal reference and cutting. Sadly, we often overlook these fundamentals of haiku. Seems to me that then or punctuation could aid in cutting. For kigo, not so sure... \$\endgroup\$ – Darren Stone Feb 18 '14 at 1:42
  • \$\begingroup\$ I am no expert to Haikus, but there is certainly some lyrical quality expected. So far I only see one answer that has any. \$\endgroup\$ – SebastianH Feb 18 '14 at 13:45

37 Answers 37

1
\$\begingroup\$

C

Least beautiful haiku ever.

int main(int argc,           
char *argv[]){int in,out;
sscanf(argv

[1],"%d",
&in);out=1;while 
(in > 

1){out*=
in--;}printf(
"%d",out);}//done

/*
Int main, int argc,
char star argv. Int in out.
Sscanf argv 

sub one, percent d, 
and in. Out equals one. While 
in is greater than

one, out times equals
in minus minus. Printf
percent d out. Done
*/
\$\endgroup\$
  • \$\begingroup\$ How do you pronounce char *argv[]){int in,out; to have it 7 syllables? I count 5-6, depending on pronounciation of argv... \$\endgroup\$ – Johannes H. Feb 18 '14 at 12:14
  • \$\begingroup\$ as in the comments: 'char star arg-v. int in out' \$\endgroup\$ – AShelly Feb 18 '14 at 14:41
  • \$\begingroup\$ Erm... totally missed the comments, was used to have them right to the code by the other answers :D Nevermind. \$\endgroup\$ – Johannes H. Feb 18 '14 at 14:42
1
\$\begingroup\$

Perl - 3 Haikus

This one useds some clever formatting and pronunciations - it also disregards symbols.

print "Enter number: ";
chomp(my $num = <STDIN>);
$result = 1;
for $x (1..$num) {
  $new = $x * $result;
  $result = $new;
}
if (1) { print "$result\n .
Is the factorial of\n";
if ($result) { print "$num"; }

Haiku:

Print: En-ter num-ber
Chomp: my num e-quals STD (stand)-IN
Re-sult e-quals one

For x one to num (or 'one range num')
New e-quals x times re-sult
re-sult e-quals new

If one: print re-sult
Is the fac-to-ri-al of
If re-sult: print num

Let me know if I messed up anywhere.

\$\endgroup\$
1
\$\begingroup\$

Mathematica - One Haiku (+1 extra (2 counting title))

factorial[n_]
 := Product @@
  Range[1, n]

fac-tor-i-al en

set de-layed pro-duct ap-ply

range from one to en

From some simple words

Complex functions can arise

Mathematica

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

Clojure

(defn fact [n] (if                  # def-fun fact n if
  (zero? n) 1                       # zero question mark n 1
  (* n (fact (dec n)))))            # times n fact dec n
\$\endgroup\$
0
\$\begingroup\$

Python 2

print(reduce(lambda              # print reduce lambda
    a,b: a * b, range(1,         # a b a times b range one
        1 + input()), 1))        # one plus input, one
\$\endgroup\$
  • \$\begingroup\$ When input is 0, TypeError: reduce() of empty sequence with no initial value \$\endgroup\$ – 200_success Feb 17 '14 at 11:27
  • \$\begingroup\$ @200_success: 0! = 1 is just a convention ;) \$\endgroup\$ – Blender Feb 17 '14 at 11:27
  • \$\begingroup\$ Now it works, but the last line has six syllables. \$\endgroup\$ – 200_success Feb 17 '14 at 12:32
0
\$\begingroup\$

Python - Two Haikus

val = int(input())        -> val is int(input())
for hi in range(1,n):   -> for hi in range one to n
    val *= hi           -> val is val times hi

# Still a cool haiku    -> Just read it
print(n)                -> print val in parentheses
# Completed haiku       -> Just read it   

Try it online!

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

Python

Uses a single haiku to do the job!

f = lambda x:   \ #  f is lambda x:
    0**x or x * \ #    zero pow x, or x times
    f(x-1)        #    f(x minus 1)

This defines a recursive lambda which calculates the factorial - 0**x handles the case of 0 as it evaluates to 1.

\$\endgroup\$

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.