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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:29:01+00:00 2026-06-18T15:29:01+00:00

Hello Folk I’m trying to create a PHP function that finds the nearest prime

  • 0

Hello Folk I’m trying to create a PHP function that finds the nearest prime number to a given integer:
for example, if you named the function “nearest_prime”, it would be used like this:

            $a = 399823;
            $b = nearest_prime($a);
            echo $b;

*Keep in mind that the integer can be any size, and that the closest prime can be above or below the integer. If two integers are equidistant from the integer, then return the lower one.

  • 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-06-18T15:29:03+00:00Added an answer on June 18, 2026 at 3:29 pm

    I’ve only tested this briefly, but it seems to work. There are definitely more efficient ways.

    function nearest_prime($num)
    {
        $up = NULL;
        $down = NULL;
        $counter = 1;
        while($up === NULL && $down === NULL)
        {
            $going_up = $num + $counter;
            $prime_up = TRUE;
            for ($k = 2;$k < $going_up;$k++)
            {
                if (($going_up % $k) === 0)
                {
                    $prime_up = FALSE;
                }
            }
            if ($prime_up === TRUE)
            {
                $up = $going_up;
            }
    
            $going_down = $num - $counter;
            $prime_down = TRUE;
            for ($k = 2;$k < $going_down;$k++)
            {
                if (($going_down % $k) === 0)
                {
                    $prime_down = FALSE;
                }
            }
            if ($prime_down === TRUE)
            {
                $down = $going_down;
            }
    
            $counter++;
        }
    
        $return = array();
        if(!is_null($up))
        {
            $return[] = $up;
        }
        if(!is_null($down))
        {
            $return[] = $down;
        }
        return implode(',',$return);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i'm trying create an application allowing me host any kind of file. In
Hello guys that is not normal :) !! foo.php <?php if (isset($_POST['data'])) $stringData =
Hello the problem I'm having is this, I have a landing page (event.php) that
Hello ! I need to create a report like the attachment shows with Jasper
Hello i'm doing a application in php, and i have a list of items
Hello I'm making a php script to extract videos URL from Youtube result. I
Hello i have simply function to read from file while(fscanf(fp, %255[a-zA-Z],test) == 1) {
Hello everyone and thanks for your help! I currently have this program that renames
Hello I'm about to create a school management system where it should assure the
Hello I'm making RSS reader and I'm using DOM. Now I stuck, trying to

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.