kennedy32
V2EX  ›  PHP

如何简化这段东西

  •  
  •   kennedy32 · Dec 13, 2013 · 6023 views
    This topic created in 4726 days ago, the information mentioned may be changed or developed.
    尝试了一下阶乘表如下:
    <?php

    function jiecheng($i){
    if($i==1){
    return $i;
    }else{
    return $i*jiecheng($i-1);
    }
    }
    function jiecheng2($i){
    if($i==1){
    return $i;
    }else{
    $j=jiecheng2($i-1);
    return "$i*$j";
    }
    }

    for($i=1;$i<=10;$i++){
    echo jiecheng2($i)."=".jiecheng($i)."<br/>";
    }

    ?>

    觉得两个功能可以合并,但是总是出错,得不到目前的结果如下:
    1=1
    2*1=2
    3*2*1=6
    4*3*2*1=24
    5*4*3*2*1=120
    6*5*4*3*2*1=720
    7*6*5*4*3*2*1=5040
    8*7*6*5*4*3*2*1=40320
    9*8*7*6*5*4*3*2*1=362880
    10*9*8*7*6*5*4*3*2*1=3628800

    求前辈指导合并,或者简化。
    Supplement 1  ·  Dec 13, 2013
    出去一天,回来看到大家的回复并且一一检测,觉得叹为观止,开阔了眼界,认识了不足,解决了问题,在此膜拜各位大神。
    55 replies  •  1970-01-01 08:00:00 +08:00
    manhere
    manhere
    Dec 13, 2013
    1
    php递归?
    kennedy32
    kennedy32
    OP
    Dec 13, 2013
    2
    @manhere 目前是两个递归,想变成一个递归,把第二个合并到第一个或者用for替代
    kavinyao
    kavinyao
    Dec 13, 2013 via iPhone
    3
    为什么不用循环?
    kennedy32
    kennedy32
    OP
    Dec 13, 2013
    4
    @kavinyao 想过,无从下手,求范例
    cyr1l
    cyr1l
    Dec 13, 2013
    5
    我去, 看这 PHP 好像 JavaScript.....
    cyr1l
    cyr1l
    Dec 13, 2013❤️1
    6
    <?php

    function sulution($num){
    for($a=1;$a<=$num;$a++){
    $b=$c=$a;
    for($d=$a;$d>0;$d--){
    if($d>=2){
    $b=$b*($d-1);
    $c=$c."*".($d-1);
    }else{
    echo $c."=".$b."<br/>";
    }
    }
    }
    }
    sulution(10);

    ?>
    尝试写了一下, 好像ok. 我PHP学的不好, 完全当成JS写的.
    txlty
    txlty
    Dec 13, 2013
    7
    <?php
    for($i=1;$i<=10;$i++){
    for($j=$i;$j>=1;$j--){
    $arr[$i][]=$j;
    }
    }
    for($i=1;$i<=10;$i++){
    echo implode($arr[$i],'*').'='.array_product($arr[$i])."\r\n";
    }
    ?>
    cyr1l
    cyr1l
    Dec 13, 2013
    8
    MD,单词还拼错了. 丢人了又...
    txlty
    txlty
    Dec 13, 2013❤️1
    9
    换成<br>
    <?php
    for($i=1;$i<=10;$i++){for($j=$i;$j>=1;$j--){$arr[$i][]=$j;}}
    for($i=1;$i<=10;$i++){echo implode($arr[$i],'*').'='.array_product($arr[$i]).'<br>';}
    ?>
    cyr1l
    cyr1l
    Dec 13, 2013
    10
    我去LSS好NB.不明觉厉.
    cyr1l
    cyr1l
    Dec 13, 2013
    11
    错了, LSSSS... 你老跟我抢楼...
    txlty
    txlty
    Dec 13, 2013❤️1
    12
    <?php
    $arr=array();
    for($i=1;$i<=10;$i++){array_unshift($arr,$i);echo implode($arr,'*').'='.array_product($arr)."<br>";}
    ?>
    总算缩成一个循环了。小站长水平,见笑。
    cyr1l
    cyr1l
    Dec 13, 2013❤️1
    13
    我去, 楼上你来劲了.
    来个Ruby版的.

    def xx(i)
    (1..i).map{|n| p (1..n).to_a.join("*")+"="+(1..n).inject(:*).to_s}
    end

    xx(10)

    应该还能更短的. 我就这水平了.
    ccidcce32167
    ccidcce32167
    Dec 13, 2013
    14
    <?php
    function jiecheng($i){
    for($a=1;$i!=1;$i--)$a*=$i;
    return $a;
    }
    ?>
    <?php
    echo jiecheng(10);
    ?>

    这个可否满足您?
    ccidcce32167
    ccidcce32167
    Dec 13, 2013
    15
    可以写成一行的

    <?php function jiecheng($i){for($a=1;$i!=1;$i--){$a*=$i;}return $a;} ?>
    y1
    y1
    Dec 13, 2013
    16
    @Livid, 支持一下代码缩进吧…… 贴 gist 的人还是不够多……
    mantianyu
    mantianyu
    Dec 13, 2013
    17
    @ccidcce32167 人家要前面那串 "10*9*8*7*6*5*4*3*2*1=" 也输出出来...
    10iii
    10iii
    Dec 13, 2013
    18
    我是来测试gist的。
    https://gist.github.com/10iii/7940276
    ccidcce32167
    ccidcce32167
    Dec 13, 2013❤️1
    20
    @mantianyu
    = =#好吧 两行
    <?php function jiecheng($i){for($a=1;$i>=1;$i--){echo $i.($i!=1?"*":"");$a*=$i;}echo "=".$a."<br />";} ?>
    <?php for($t=1;$t<=10;$t++){jiecheng($t);} ?>
    zhujinliang
    zhujinliang
    Dec 13, 2013❤️1
    21
    没人想到用神器eval么。。。
    不过看起来比楼上某些同学的更长。。。

    while($i++ < 10)
    {
    $s=$i.$p.$s;
    $p='*';
    echo "$s=".eval("return $s;").'<br/>';
    }

    顺便测试了php对未定义变量的容忍程度,居然未初始化的int与string都可以通过并按预想地执行。
    zhujinliang
    zhujinliang
    Dec 13, 2013❤️1
    22
    次奥,玩上瘾了。。。
    目前是不是我的最短~~(°∀°)ノ

    <?php for(;$s=++$i.$p.$s,$i<11;$p='*')eval("echo '$s='.($s).'<br>';");

    除去<?php部分,长度是64字节
    thbourlove
    thbourlove
    Dec 13, 2013
    24
    这么多人喜欢把php当perl用吗?
    Ever
    Ever
    Dec 13, 2013❤️1
    25
    来个fp点的吧

    <?php

    function multiplication_table($max=10, $current=1, $cache_expr="", $cache_result=1, $row_sep="\n", $result=array()){
    $expr = $current ===1 ? $current: "{$current} * {$cache_expr}";
    $cache_result *= $current++;
    $result[] = "$expr = {$cache_result}";
    return $current<=$max? multiplication_table($max, $current, $expr, $cache_result, $row_sep, $result):join($row_sep, $result);
    }

    echo multiplication_table(10);
    echo "\n";
    msg7086
    msg7086
    Dec 13, 2013❤️2
    26
    https://gist.github.com/msg7086/7941064

    考虑到div宽度所以换行了。比gmp应该要慢一些,但是算是全内置函数实现了
    thbourlove
    thbourlove
    Dec 13, 2013
    27
    @msg7086 快慢其实根本测不出。。不用gmp到20+就果断溢出了。。
    msg7086
    msg7086
    Dec 13, 2013❤️2
    28
    https://gist.github.com/msg7086/7941111
    这个是带中间变量的版本,递增运算所以速度会很快

    @thbourlove 溢出无解吧╮(╯_╰)╭
    10iii
    10iii
    Dec 13, 2013
    29
    这样?
    <script src="https://gist.github.com/10iii/7940276.js"></script>
    10iii
    10iii
    Dec 13, 2013
    31
    Fedor
    Fedor
    Dec 13, 2013
    32
    Fedor
    Fedor
    Dec 13, 2013❤️1
    33
    @10iii

    https://gist.github.com/thbourlove/7940940.js 不要后面的.js就可以了...
    Geeker
    Geeker
    Dec 13, 2013
    34
    刚学PHP,看了楼上的各种代码,整个人都不好了ORZ
    infong
    infong
    Dec 13, 2013
    35
    看完楼上们的回复,感觉整个都不知道php是什么了。
    mikej
    mikej
    Dec 13, 2013
    36
    这帖子很v2ex
    mantianyu
    mantianyu
    Dec 13, 2013
    38
    mantianyu
    mantianyu
    Dec 13, 2013
    39
    这下可以了吧
    <script src="https://gist.github.com/cifer-lee/7946292"></script>
    mantianyu
    mantianyu
    Dec 13, 2013
    40
    再来一次
    <script src="https://gist.github.com/thbourlove/7940940.js"></script>
    mantianyu
    mantianyu
    Dec 13, 2013
    41
    我了个去
    <script src="https://gist.github.com/thbourlove/7940940"></script>
    mantianyu
    mantianyu
    Dec 13, 2013
    42
    mantianyu
    mantianyu
    Dec 14, 2013
    43
    T_T_T_T_T_T_T_T_T_T_T

    <script src="https://gist.github.com/cifer-lee/7946292.js"></script>
    mantianyu
    mantianyu
    Dec 14, 2013
    44
    <?php
    // 递归版
    $s = 1;
    function jiecheng($num) {
    global $s; $s *= $num;
    if($num == 1) return "$num = $s \n"; else return "$num * " . jiecheng($num - 1);
    }

    // 循环版
    function jiecheng2($num) {
    for($m=1,$s=1,$i=1;$i<$num;++$i) {$m=($i+1)." * $m";$s*=($i+1);}echo "$m = $s\n";
    }
    octref
    octref
    Dec 14, 2013
    45
    function factorial($x) {
    for($i=1, $j=1; $i <= $x; $j*=$i, $i++);
    return $j;
    }
    echo factorial(10)
    picasso250
    picasso250
    Dec 14, 2013
    46
    我看不下去了,幫你貼個
    https://gist.github.com/cifer-lee/7946292
    yangff
    yangff
    Dec 15, 2013
    48
    @picasso250 噗。。
    thbourlove
    thbourlove
    Dec 15, 2013
    49
    已笑尿。。。
    faceair
    faceair
    Dec 15, 2013
    50
    @mantianyu 我来试贴一下,转换不出来不要笑。。 https://gist.github.com/cifer-lee/7946292
    faceair
    faceair
    Dec 15, 2013
    51
    我已经尽力了。。
    谁知道贴gist有什么诀窍 @Livid
    mantianyu
    mantianyu
    Dec 15, 2013
    52
    @faceair
    @picasso250

    奇怪了, 我贴了两个别人的, 就成功了, 贴我自己的, 就是成功不了.
    mantianyu
    mantianyu
    Dec 15, 2013
    53
    thwawar
    thwawar
    Dec 15, 2013
    55
    直接贴那个 gist 在地址栏的地址就好了。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   929 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 20:59 · PVG 04:59 · LAX 12:59 · JFK 15:59
    ♥ Do have faith in what you're doing.