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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:14:30+00:00 2026-06-16T17:14:30+00:00

I need to find the highest prime number in a given range. Here is

  • 0

I need to find the highest prime number in a given range.
Here is my code which works for 0-100 but if I give 0-125 it is showing prime number as 125.

<?php
    $flag=0;
    $b=125;
    for($i=$b;$i>=0;$i--)
    {
        if($i%2!=0)
        {
            for($b=3;$b<10;$b++)
            {
                if($flag==0)
                {
                    echo('<br>');
                    if($i%$b!=0)
                    {
                        echo('highest prime number is'.$i);
                        $flag=1;
                        break;
                    }
                    elseif ($i%$b==0)
                    {
                        break;
                    }
                }
            }
        }
    }
?>

In the above code I have taken the range from 0-125

  • 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-16T17:14:32+00:00Added an answer on June 16, 2026 at 5:14 pm

    Yup the problem is algorithmic…

    1) You’ll need to check up till sqrt($b) i.e. 11 in this case

    2) The $flag logic is kinda messed up, no use changing the flag then breaking out right after…

    <?php
            $flag=0;
            $b=125;
            $sq=sqrt($b); 
    
            for($i=$b;$i>=0;$i--)
            {
    
                if($i%2!=0)
                {
                        for($b=3;$b<=$sq;$b++)
                        {
                                if($i%$b!=0)
                                {
                                    $flag=1;
                                }
                                elseif($i%$b==0)
                                {
                                    $flag=0;
                                    break;
                                }
                        }
                if($flag==1){ 
                    echo('highest prime number is '.$i);  
                    break;
                }
            }
        }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an integer array, i need to find which number occurred most number of
I need to find the highest number from 3 different numbers. The only thing
I have the following List<int> collection and I need to find the highest integer
So, I need to find out the highest possible difference between any two random
I need to find out the value passed into an indexer. My code (c#)
I need to find the highest order 1 in some longs, ints, and shorts
There is an ArrayList which stores integer values. I need to find the maximum
So i need to find the tribe (table called pleme) with highest population.. to
I need to find a way to get the data with the highest versionNumber.
I was finding out highest prime factor which divides num, as shown in program,

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.