www.webdeveloper.com
Results 1 to 4 of 4

Thread: need help with a simple logic

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    47

    need help with a simple logic

    Hi friends

    i was trying to generate a simple mathematical series as follows

    1
    1,2
    2,3,4
    4,5,6,7
    7,8,9,10,11

    tried at least half a day with no result. i messed up everything, now its totally blank in my head

    can any body help me on this

    thanks in advance

  2. #2
    Join Date
    Feb 2012
    Posts
    218
    PHP Code:
        function nrSum($index){
            
    $sum 0;
            for(
    $i=0$i<=$index$i++){
                
    $sum+=$i;
            }
            return 
    $sum;
        }
        for(
    $i=0$i<10$i++){
            
    $start nrSum($i-1)+1;
            for(
    $j=0$j<=$i$j++){
                echo (
    $start+$j).", ";
            }
            echo 
    "<br />";
        } 

  3. #3
    Join Date
    Jan 2007
    Posts
    47
    Thank you very much hyperionXS

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    Actually I found a shorter one:
    PHP Code:
        for($i=0$i<10$i++){
            
    $start $i * ($i-1) / 1;
            for(
    $j=0$j<=$i$j++){
                echo (
    $start+$j).", ";
            }
            echo 
    "<br />";
        } 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles