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 7623785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:47:34+00:00 2026-05-31T04:47:34+00:00

This code is taking like 15mins to complete, is there anyway it could be

  • 0

This code is taking like 15mins to complete, is there anyway it could be optimized?

<?php 
$base = 99;
$results = 1975;
$goal = 1000001;
while ($results <= $goal)
{   

    for ( $i=0; $i <= $base; $i++ )
    {
        for ( $j=$i+1; $j <  $base ; $j++  )
        {   

            $hypo = sqrt(( pow($i + $j, 2) )+ pow($base, 2));                   

            if ($hypo == (int) $hypo )
            {
                if ($results == $goal)  
                {
                    echo $i, ' ', $j, ' ',$base , '
';
                    break 3;
                }  
                else
                {
                    $results++;
                }
            }
        }       
    }
    $base++;    
}
echo $base;
?>
  • 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-31T04:47:37+00:00Added an answer on May 31, 2026 at 4:47 am

    In a tight loop that’s iterated so many times, preincrement is going to be faster than postincrement.
    And calling pow() has the overheads of the function call, faster to do the math directly.

    $base = 99;
    $results = 1975;
    $goal = 1000001;
    while ($results <= $goal)
    {
    
        for ( $i=0; $i <= $base; ++$i )
        {
            for ( $j=$i+1; $j <  $base ; ++$j )
            {
    
                $hypo = sqrt(( ($i + $j) * ($i + $j) ) + ($base * $base));
    
                if ($hypo == (int) $hypo )
                {
                    if ($results == $goal)
                    {
                        echo $i, ' ', $j, ' ',$base , '
    ';
                        break 3;
                    }
                    else
                    {
                        ++$results;
                    }
                }
            }
        }
        ++$base;
    }
    echo $base;
    

    That still won’t be fast because of the sheer number of iterations, but those two minor changes should reduce the time taken quite significantly… think about 50%-75% faster.

    But if you explain what you’re actually trying to do – it looks like you’re trying to work out pythagorean triangle numbers – perhaps there are more efficient methods than brute force.

    EDIT

    Performance when executed with $goal = 10001:

    Call time for postincrement and pow() method was 12.4709 seconds
    Call time for preincrement and pow() method was 12.4239 seconds
    Call time for preincrement method and multiply was 4.9315 seconds
    

    SO you can see that postincrement -> preincrement reduces by about 0.05 seconds, but replacing the call to pow() by doing the math directly makes the most significant difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
This bit of code is taking almost a half second to execute. Could somebody
I want to process every element of a for-loop. Taking this code, why is
It is taking .6-.8 seconds for this line of my code to execute in
There is this example code, but then it starts talking about millisecond / nanosecond
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
Is there a better way of writing this code? It just doesn't sit right
I got 12 .php files with this code: if($_GET['u']){ $srcp = imagecreatefrompng(http://www.minecraft.net/skin/.$_GET['u']..png); $destp =
I have this code which is taking the name of files in a directory
To be specific, I'm talking about avoiding this type of code: <input type='text' id='title_33'

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.