V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kennedy32
V2EX  ›  PHP

如何简化这段东西

  •  
  •   kennedy32 · 2013-12-13 01:02:47 +08:00 · 4728 次点击
    这是一个创建于 3787 天前的主题,其中的信息可能已经有所发展或是发生改变。
    尝试了一下阶乘表如下:
    <?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

    求前辈指导合并,或者简化。
    第 1 条附言  ·  2013-12-13 20:18:06 +08:00
    出去一天,回来看到大家的回复并且一一检测,觉得叹为观止,开阔了眼界,认识了不足,解决了问题,在此膜拜各位大神。
    55 条回复    1970-01-01 08:00:00 +08:00
    manhere
        1
    manhere  
       2013-12-13 01:08:42 +08:00
    php递归?
    kennedy32
        2
    kennedy32  
    OP
       2013-12-13 01:09:34 +08:00
    @manhere 目前是两个递归,想变成一个递归,把第二个合并到第一个或者用for替代
    kavinyao
        3
    kavinyao  
       2013-12-13 01:29:20 +08:00 via iPhone
    为什么不用循环?
    kennedy32
        4
    kennedy32  
    OP
       2013-12-13 01:36:51 +08:00
    @kavinyao 想过,无从下手,求范例
    cyr1l
        5
    cyr1l  
       2013-12-13 01:48:44 +08:00
    我去, 看这 PHP 好像 JavaScript.....
    cyr1l
        6
    cyr1l  
       2013-12-13 02:22:01 +08:00   ❤️ 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  
       2013-12-13 02:22:38 +08:00
    <?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  
       2013-12-13 02:23:21 +08:00
    MD,单词还拼错了. 丢人了又...
    txlty
        9
    txlty  
       2013-12-13 02:24:39 +08:00   ❤️ 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  
       2013-12-13 02:25:27 +08:00
    我去LSS好NB.不明觉厉.
    cyr1l
        11
    cyr1l  
       2013-12-13 02:26:56 +08:00
    错了, LSSSS... 你老跟我抢楼...
    txlty
        12
    txlty  
       2013-12-13 03:01:50 +08:00   ❤️ 1
    <?php
    $arr=array();
    for($i=1;$i<=10;$i++){array_unshift($arr,$i);echo implode($arr,'*').'='.array_product($arr)."<br>";}
    ?>
    总算缩成一个循环了。小站长水平,见笑。
    cyr1l
        13
    cyr1l  
       2013-12-13 03:10:59 +08:00   ❤️ 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  
       2013-12-13 10:55:11 +08:00
    <?php
    function jiecheng($i){
    for($a=1;$i!=1;$i--)$a*=$i;
    return $a;
    }
    ?>
    <?php
    echo jiecheng(10);
    ?>

    这个可否满足您?
    ccidcce32167
        15
    ccidcce32167  
       2013-12-13 10:56:20 +08:00
    可以写成一行的

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

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

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

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

    除去<?php部分,长度是64字节
    thbourlove
        23
    thbourlove  
       2013-12-13 15:24:41 +08:00   ❤️ 2
    thbourlove
        24
    thbourlove  
       2013-12-13 15:29:06 +08:00
    这么多人喜欢把php当perl用吗?
    Ever
        25
    Ever  
       2013-12-13 15:44:26 +08:00   ❤️ 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  
       2013-12-13 15:47:29 +08:00   ❤️ 2
    https://gist.github.com/msg7086/7941064

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

    @thbourlove 溢出无解吧╮(╯_╰)╭
    10iii
        29
    10iii  
       2013-12-13 17:04:17 +08:00
    这样?
    <script src="https://gist.github.com/10iii/7940276.js"></script>
    10iii
        30
    10iii  
       2013-12-13 17:17:24 +08:00
    10iii
        31
    10iii  
       2013-12-13 17:18:18 +08:00
    Fedor
        32
    Fedor  
       2013-12-13 17:22:06 +08:00
    Fedor
        33
    Fedor  
       2013-12-13 17:22:53 +08:00   ❤️ 1
    @10iii

    https://gist.github.com/thbourlove/7940940.js 不要后面的.js就可以了...
    Geeker
        34
    Geeker  
       2013-12-13 18:04:41 +08:00
    刚学PHP,看了楼上的各种代码,整个人都不好了ORZ
    infong
        35
    infong  
       2013-12-13 21:10:05 +08:00
    看完楼上们的回复,感觉整个都不知道php是什么了。
    mikej
        36
    mikej  
       2013-12-13 21:58:32 +08:00
    这帖子很v2ex
    mantianyu
        37
    mantianyu  
       2013-12-13 23:51:06 +08:00
    mantianyu
        38
    mantianyu  
       2013-12-13 23:51:34 +08:00
    mantianyu
        39
    mantianyu  
       2013-12-13 23:54:17 +08:00
    这下可以了吧
    <script src="https://gist.github.com/cifer-lee/7946292"></script>
    mantianyu
        40
    mantianyu  
       2013-12-13 23:56:32 +08:00
    再来一次
    <script src="https://gist.github.com/thbourlove/7940940.js"></script>
    mantianyu
        41
    mantianyu  
       2013-12-13 23:56:55 +08:00
    我了个去
    <script src="https://gist.github.com/thbourlove/7940940"></script>
    mantianyu
        42
    mantianyu  
       2013-12-13 23:58:12 +08:00
    mantianyu
        43
    mantianyu  
       2013-12-14 00:01:27 +08:00
    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  
       2013-12-14 00:01:56 +08:00
    <?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  
       2013-12-14 00:33:38 +08:00
    function factorial($x) {
    for($i=1, $j=1; $i <= $x; $j*=$i, $i++);
    return $j;
    }
    echo factorial(10)
    picasso250
        46
    picasso250  
       2013-12-14 13:17:23 +08:00
    我看不下去了,幫你貼個
    https://gist.github.com/cifer-lee/7946292
    picasso250
        47
    picasso250  
       2013-12-14 13:17:49 +08:00
    yangff
        48
    yangff  
       2013-12-15 13:27:32 +08:00
    @picasso250 噗。。
    thbourlove
        49
    thbourlove  
       2013-12-15 16:01:43 +08:00
    已笑尿。。。
    faceair
        50
    faceair  
       2013-12-15 20:44:10 +08:00
    @mantianyu 我来试贴一下,转换不出来不要笑。。 https://gist.github.com/cifer-lee/7946292
    faceair
        51
    faceair  
       2013-12-15 20:45:34 +08:00
    我已经尽力了。。
    谁知道贴gist有什么诀窍 @Livid
    mantianyu
        52
    mantianyu  
       2013-12-15 20:47:39 +08:00
    @faceair
    @picasso250

    奇怪了, 我贴了两个别人的, 就成功了, 贴我自己的, 就是成功不了.
    mantianyu
        53
    mantianyu  
       2013-12-15 20:49:52 +08:00
    thwawar
        54
    thwawar  
       2013-12-15 22:28:10 +08:00
    thwawar
        55
    thwawar  
       2013-12-15 22:28:47 +08:00
    直接贴那个 gist 在地址栏的地址就好了。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2498 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 776ms · UTC 16:01 · PVG 00:01 · LAX 09:01 · JFK 12:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.