Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7505171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:49:03+00:00 2026-05-29T21:49:03+00:00

so I am new at the idea of recursion and i wrote this simple

  • 0

so I am new at the idea of recursion and i wrote this simple code to factor a number ($n) this is the code:

$n = 120;
$y = 1;

function factor($n, $y) {
    if($y > $n) {
        return 1;
    } else {
        $x = $n / $y;
        list($whole, $dec) = array_pad(explode('.', $x), 2, Null);
        if($dec == '') {
            echo 'x:' . $x . ' y:' . $y . '</br>';
            return factor($n, ($y + 1));
        }
    }
}

this is what the code outputs:

x:120 y:1
x:60 y:2
x:40 y:3
x:30 y:4
x:24 y:5
x:20 y:6

so my question is why does this stop before it completes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:49:03+00:00Added an answer on May 29, 2026 at 9:49 pm

    Your next step would be 120 / 7 which equals 17.142857….

    So this check fails and as such the recursion does not happen:

    if($dec=='') // $dec would equal to 142857.....
    {
        echo'x:'.$x.' y:'.$y.'</br>';
        return factor($n,($y+1));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to the idea of recursion and this is actually my first
I'm very new to C, but have no idea why this program breaks. The
HTML5 will be next super star. So~~~How to using new idea to implement AJAX
I'm new to IDEA, but very familiar with Eclipse. I followed the instructions here
I am new to intellij idea and I decided to shift because I found
So i'm new to the idea of inheritance and interfaces and i'm not sure
I'm new and I have no idea where the default directory for the open()
When creating a new Java project in IntelliJ IDEA, the following directories and files
Is it a good idea to start new test framework using Selenium web driver
Hi I'm new to Linux and I really like the idea of writing and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.