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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:09:46+00:00 2026-06-05T10:09:46+00:00

I am having a problem where my program executes and then just hangs somewhere

  • 0

I am having a problem where my program executes and then just hangs somewhere during execution. I basically want to know what causes this.

for (long i = maxNumber; i > 2; i--)
{
    IsPrime = true;
    for (long g = 2; g < i; g++)
    {
        long temp = i % g;
        if (temp == 0)
        {
            IsPrime = false;                            
            break;
        }
    }
    if (IsPrime == true)
    {
        largestPrimeFactor = i;                        
        break;
    }
}
  • 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-05T10:09:48+00:00Added an answer on June 5, 2026 at 10:09 am

    If I tried it correctly, your code is trying to find the largest prime between 0 and maxNumber. Use the Sieve of Eratosthenes for finding all prime numbers between 0 and the square root of maxNumber. Then, you can iterate from maxNumber to 0 for a number indivisible by all of the prime numbers you’ve just found.

    EDIT :
    Tried this

                var sqrtMax = (int)Math.Sqrt(maxNumber);
                var primeCandidates = Enumerable.Range(2, sqrtMax-1)
                 .ToDictionary(number => number, isComposite => false);
    
                foreach (var number in primeCandidates.Keys.ToArray())
                {
                    if (primeCandidates[number])
                    {
                        continue;
                    }
                    Parallel.ForEach(Enumerable.Range(2, sqrtMax / number - 1).Select(times => number * times),multiples=>
                        primeCandidates[multiples] = true);
                }
                var primeList = primeCandidates.Where(number => !number.Value).Select(pair=>pair.Key).ToArray();
                var maxPrime = maxNumber;
    
    
                while (primeList.AsParallel().Any(prime=> maxPrime%prime==0))
                {
                    maxPrime--;
                }
    

    and it find maxPrime in less than 3 seconds for maxNumber = 600881475134 (the parallelization was because I thought it would take long time)

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

Sidebar

Related Questions

I wrote this code because I'm having a similar problem in a larger program
I have been having this problem wherein my threads stop execution and go into
So I'm having issues with my program. Basically this far the program is like
I'm having problem when running my Windows Forms program. In the program, I have
I'm having a very odd problem with my program and am hoping you can
Im having a very odd problem. Im trying to install a Python program. Im
all. I'm having a bit of weird problem with client server program. I have
I'm hoping someone can help me with this annoying little problem I'm having. I'm
I am having a problem with the example from this article . The article
I'm having a problem with a program I'm developing (you can see it at

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.