首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >柴歌中的阶乘!

柴歌中的阶乘!
EN

Code Golf用户
提问于 2014-02-17 01:29:59
回答 29查看 9.1K关注 0票数 63

任务

创建一个程序,使用不内置的阶乘函数计算数字的阶乘。容易吗?问题是,您必须以柴歌形式编写整个程序(包括测试程序)。

你可以随心所欲地使用多少句话,但发音时,它们必须遵循5-7-5音节的格式。

评分

这是一个人气-竞赛,所以你必须赢得最多的选票。你的程序必须包括至少一个完整的haiku,并且所有的haikus必须是完整的。

当阅读代码时,每句话的第一行将有5个音节,第二行将有7个音节,第三行将有5个音节。

EN

回答 29

Code Golf用户

回答已采纳

发布于 2014-02-17 03:30:49

Smalltalk

(在工作区中进行评估;打开一个对话框,请求一个数字并在stdout上打印结果):

代码语言:javascript
复制
"in" "this" 'poem,' "you" "must"
"pronounce" "characters" "like" "these:"
"return(^)," "and" "times(*);" "please".

"but" 'never' "in" "here"
"tell" "anyone" "about" "those"
"few" "parentheses".

"otherwise" "these" "words" 
"are" "stupid" "and" "this" "coded" 
"rhyme" "is" "wasted" Time.

"let" "us" "now" "begin" 
"by" "defining," "in" Object
"and" compile: "the" "rhyme:"

'fac: "with" arg"ument"
"to" "compare" arg <"against" 2 ">"
"and" ifTrue: [ ^"return"

"["1] "or" ifFalse: "then"
["return"^ arg *"times" "the" "result"
"of" ("my"self ")getting"

"the" fac:"torial"
"of" "the" "number" arg "minus"-
1 "[(yes," "its" "easy")]'.

("Let" "me" "my"self "ask"
"for" "a" "number," "to" "compute"
"the" fac:"torial"

("by" "opening" "a" 
"nice" Dialog "which" "sends" "a"
request: "asking" "for"

'the Number to use' 
"(which" "is" "(" "treated" ) asNumber)
"then" print "the" "result".

我也试着带来一些反思(“在这首诗中”)和奇戈。此外,还包括一些西方风格的押韵元素(请->这些,时间->押韵);但是,既不是以日语为母语,也不是英语,请原谅任何文体细节;-)

票数 57
EN

Code Golf用户

发布于 2014-02-17 02:10:39

Java -2 haikus

代码语言:javascript
复制
protected static
        int factorial(int n) {
    if (n == 0) {
        return n + 1;
    } return factorial(n
            - 1) * n;}

即使问题不是密码-高尔夫,我也经常发现自己在打高尔夫球。在这种情况下,我打出了猜句的数量。

我宣布如下:

受保护的静态整数乘整数n如果n为零返回n+1 返回阶乘n减去1乘n

测试程序:

代码语言:javascript
复制
class Factorial {                                    // class Factorial
    public static void main(String[]                 // public static void main string
            command_line_run_args) {                 // command line run args
        
        int i = 0;                                   // int i is zero
        while (7 != 0)                               // while seven is not zero
            System.out.                              // System dot out dot

                    println(i + "!"                  // print line i plus not
                            + " = " + factorial(     // plus is plus factorial
                            i += 1));}               // i plus equals 1

    protected static
            int factorial(int n) {
        if (n == 0) {
            return n + 1;
        } return factorial(n
                - 1) * n;}}

注意,这个程序开始快速输出0s,这是溢出的结果。通过将每个int更改为long,您可以很容易地获得更大的正确数字。

System.out.printlnpublic static void main(String[] args)的标准发音反映在程序中。

票数 41
EN

Code Golf用户

发布于 2014-02-17 02:22:36

Haskell

代码语言:javascript
复制
fact :: Int -> Int          -- fact is Int to Int
fact x = product (range x)  -- fact x is product range x
range x = [1..x]            -- range x is 1 [pause] x

Haskell教育时间:

  • range x函数创建一个从1到x值的整数列表。
  • fact x函数将列表range x的所有值相乘,以计算结果。
  • 第一行说,fact函数接受一个整数并返回一个整数。
票数 41
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/21095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档