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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:41:10+00:00 2026-05-28T07:41:10+00:00

I have a function that gives me a number as result: Now to make

  • 0

I have a function that gives me a number as result:

Now to make it simple I’ll use an invented example:

   function generate($a) {return $a*2;}
   //this is just an example, the real generate function is really expensive in terms of speed and resources

I have also an array with the values to pass to that funcion:

$array = array(1,3,4,6,8,9,11);

I’d like to find the value of $array, that, passed to generate(), gives as output the number nearest to 5 and minor of it.

With a progressive search I would get this:

$array[0] => 2;
$array[1] => 6;
$array[2] => 8;
etc.

In this case I’d expect my search function to give as output the number 1 as this is value that, passed into generate(), gives 2 as output, the number nearest and minor to 5.

Since the generate() function is really slow ( 1,5 sec on average ) I want to do a binary search with the hope of reducing the use of my function.

So basically what I want to do is: slice $array into 2 pieces, use generate(), then slice againg etc.

I’m not an expert both in recursive function and binary search (it’s my first script trying to do that).

However I tried to wrote some code I’m pasting below, however it doesn’t work and honestrly I don’t have a clear idea about it.

function generate($a) {return $a*2;}

$array = array(1,2,3,4,5,6,7,8,9);

function find($array) {
    $first_half = array_slice($array,0,round(count($array)/2,0));
    $second_half = array_slice($array,round(count($array)/2,0));
    echo "<pre>";
    print_r($first_half);
    print_r($second_half);
    echo "</pre>";
    $last = end($first_half);
    $last = generate($last);

    if($last > 4) {find($first_half);}
    else {}
}

find($array);

Can you help me?

Best regards,
Giorgio

  • 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-28T07:41:10+00:00Added an answer on May 28, 2026 at 7:41 am

    While this may be a good try for some academic research, in practice I think it’s slower than a plain progressive approach.

    What you would want is to optimize generate() (or the way it is called*) rather than the traversal algorithm.


    *One such improvement is to replace this:

    foreach($array as $k=>$v) $array[$k] = generate($v);
    

    With this:

    $array = array_map('generate', $array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function that gets a given number of random records from
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that I use called sqlf(), it emulates prepared statements. For
I have the following function that I use to check a user login, at
I have a function that takes an object as an argument and gives me
I have a function that processes a given vector, but may also create such
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have a function that gets x(a value) and xs(a list) and removes all

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.