kennedy32
V2EX  ›  PHP

如何简化这段东西

  •  
  •   kennedy32 · Dec 13, 2013 · 5649 views
    This topic created in 4564 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
        1
    manhere  
       Dec 13, 2013
    php递归?
    kennedy32
        2
    kennedy32  
    OP
       Dec 13, 2013
    @manhere 目前是两个递归,想变成一个递归,把第二个合并到第一个或者用for替代
    kavinyao
        3
    kavinyao  
       Dec 13, 2013 via iPhone
    为什么不用循环?
    kennedy32
        4
    kennedy32  
    OP
       Dec 13, 2013
    @kavinyao 想过,无从下手,求范例
    cyr1l
        5
    cyr1l  
       Dec 13, 2013
    我去, 看这 PHP 好像 JavaScript.....
    cyr1l
        6
    cyr1l  
       Dec 13, 2013   ❤️ 1
    <?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
        7
    txlty  
       Dec 13, 2013
    <?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
        8
    cyr1l  
       Dec 13, 2013
    MD,单词还拼错了. 丢人了又...
    txlty
        9
    txlty  
       Dec 13, 2013   ❤️ 1
    换成<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
        10
    cyr1l  
       Dec 13, 2013
    我去LSS好NB.不明觉厉.
    cyr1l
        11
    cyr1l  
       Dec 13, 2013
    错了, LSSSS... 你老跟我抢楼...
    txlty
        12
    txlty  
       Dec 13, 2013   ❤️ 1
    <?php
    $arr=array();
    for($i=1;$i<=10;$i++){array_unshift($arr,$i);echo implode($arr,'*').'='.array_product($arr)."<br>";}
    ?>
    总算缩成一个循环了。小站长水平,见笑。
    cyr1l
        13
    cyr1l  
       Dec 13, 2013   ❤️ 1
    我去, 楼上你来劲了.
    来个Ruby版的.

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

    xx(10)

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

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

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

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

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

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

    除去<?php部分,长度是64字节
    thbourlove
        23
    thbourlove  
       Dec 13, 2013   ❤️ 2
    thbourlove
        24
    thbourlove  
       Dec 13, 2013
    这么多人喜欢把php当perl用吗?
    Ever
        25
    Ever  
       Dec 13, 2013   ❤️ 1
    来个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
        26
    msg7086  
       Dec 13, 2013   ❤️ 2
    https://gist.github.com/msg7086/7941064

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

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

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

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

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