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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:29:02+00:00 2026-06-10T14:29:02+00:00

I want a function called findFirst that takes parameters n and q and returns

  • 0

I want a function called findFirst that takes parameters n and q and returns the smallest prime number dividing n that is greater than or equal to q. So first I wrote a function that would say if a number is prime or not.

var isPrime = function(n){
    if(n === 1){
        return false;
    }
    else if (n === 2 || n === 3){
        return true;
    }
    else {
        for(i=2; i < n; i++){
            if(i * i >= n){
            for(j=2; j<=i; j++){
                    if(n % j === 0){
                        return false;
                    }
                }
            return true;
            } 
        }
    }
};

There could be other ways to make this more efficient, but I am pretty sure this function is not the problem.

So with this function I made my first attempt to write findFirst:

var findFirst = function(n,q){
    var p = q;
        while(n % p !== 0 || isPrime(p) === false){
            p++;
        }
    return p;
};

This function works, but with large numbers it crashes, for example it crashes on input (6310545154, 3). By the way the prime power decomposition of 6310545154 is 2 * 3155272577

So I wrote another function that would first just list the prime factors of a number:

var getFactors = function(n){
    var factors = [];
    var k = n;
    var p = 2;
    while(isPrime(k) === false && k !== 1){
        while(k % p !== 0 || isPrime(p) === false){
            p = p+1;
        }
        while(k % p === 0){
            k = k/p;
        }
        factors.push(p);
    }
    if(isPrime(k) === true){
        factors.push(k);
        return factors;
    }
    if(k === 1){
        return factors; 
    }
};

And now my second attempt at findFirst is:

var findFirst = function(n,q){
    var array = getFactors(n);
    var p = 0;
    for(i = 0; i < array.length; i++){
        if(array[i] >= q && p === 0){
            p = array[i];
        }
    }
    return p;
};

This one works great. It doesn’t crash on that large number above and computes the result instantly. Can anyone see why this would happen? It seems like the ‘while’ loop in my original attempt is pretty similar to one of the ‘while’ loops in the getFactors function. And the second attempt looks a lot more complicated.

  • 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-10T14:29:04+00:00Added an answer on June 10, 2026 at 2:29 pm

    The second program returns very quickly because your number only has one large prime factor; once it’s found (all) the small prime factor(s) it quickly exits its loop. The first program has to count all the way from 3 up to 3155272577 before it discovers that it’s a factor. After 2147483647 it has to switch from integer to floating-point arithmetic, which makes it even slower.

    Note that

    var isPrime = function(n) {
        ...
    };
    

    is an unusual way of creating a function; normally you would write

    function isPrime(n) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function called FindSpecificRowValue that takes in a datatable and returns the
I want to use an initialization function that will be called after a user
I just want a function that can take 2 parameters: the URL to POST
Say I want a function that takes two floats ( x and y ),
I want to make a function called debug that outputs some info about objects.
I use a function called count_days($date1,$date2) that counts the number of days between two
I have a function called insert which takes two parameters ( name and telnumber
I want this function (defn ret-lowest-str-len Computes the lengths of two strings. Returns default
I'm trying to call a function that contains jQuery code. I want this function
I have a function called GetUserByOpenId I don't want to be using this function

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.