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

  • Home
  • SEARCH
  • 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 7824063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:28:21+00:00 2026-06-02T08:28:21+00:00

int prime(unsigned long long n){ unsigned val=1, divisor=7; if(n==2 || n==3) return 1; //n=2,

  • 0
int prime(unsigned long long n){
    unsigned val=1, divisor=7;
    if(n==2 || n==3) return 1; //n=2, n=3 (special cases).
    if(n<2 || !(n%2 && n%3)) return 0; //if(n<2 || n%2==0 || n%3==0) return 0;
    for(; divisor<=n/divisor; val++, divisor=6*val+1) //all primes take the form 6*k(+ or -)1, k[1, n).
        if(!(n%divisor && n%(divisor-2))) return 0; //if(n%divisor==0 || n%(divisor-2)==0) return 0;
    return 1;
}

The code above is something a friend wrote up for getting a prime number. It seems to be using some sort of sieving, but I’m not sure how it exactly works. The code below is my less awesome version. I would use sqrt for my loop, but I saw him doing something else (probably sieving related) and so I didn’t bother.

int prime( unsigned long long n ){
    unsigned i=5;
    if(n < 4 && n > 0)
        return 1;
    if(n<=0 || !(n%2 || n%3))
        return 0;
    for(;i<n; i+=2)
        if(!(n%i)) return 0;
    return 1;
}

My question is: what exactly is he doing?

  • 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-02T08:28:23+00:00Added an answer on June 2, 2026 at 8:28 am

    Your friend’s code is making use of the fact that for N > 3, all prime numbers take the form (6×M±1) for M = 1, 2, … (so for M = 1, the prime candidates are N = 5 and N = 7, and both those are primes). Also, all prime pairs are like 5 and 7. This only checks 2 out of every 3 odd numbers, whereas your solution checks 3 out of 3 odd numbers.

    Your friend’s code is using division to achieve something akin to the square root. That is, the condition divisor <= n / divisor is more or less equivalent to, but slower and safer from overflow than, divisor * divisor <= n. It might be better to use unsigned long long max = sqrt(n); outside the loop. This reduces the amount of checking considerably compared with your proposed solution which searches through many more possible values. The square root check relies on the fact that if N is composite, then for a given pair of factors F and G (such that F×G = N), one of them will be less than or equal to the square root of N and the other will be greater than or equal to the square root of N.


    As Michael Burr points out, the friend’s prime function identifies 25 (5×5) and 35 (5×7) as prime, and generates 177 numbers under 1000 as prime whereas, I believe, there are just 168 primes in that range. Other misidentified composites are 121 (11×11), 143 (13×11), 289 (17×17), 323 (17×19), 841 (29×29), 899 (29×31).

    Test code:

    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long c;
    
        if (prime(2ULL))
            printf("2\n");
        if (prime(3ULL))
            printf("3\n");
        for (c = 5; c < 1000; c += 2)
            if (prime(c))
                printf("%llu\n", c);
        return 0;
    }
    

    Fixed code.

    The trouble with the original code is that it stops checking too soon because divisor is set to the larger, rather than the smaller, of the two numbers to be checked.

    static int prime(unsigned long long n)
    {
        unsigned long long val = 1;
        unsigned long long divisor = 5;
    
        if (n == 2 || n == 3)
            return 1;
        if (n < 2 || n%2 == 0 || n%3 == 0)
            return 0;
        for ( ; divisor<=n/divisor; val++, divisor=6*val-1)
        {
            if (n%divisor == 0 || n%(divisor+2) == 0)
                return 0;
        }
        return 1;
    }
    

    Note that the revision is simpler to understand because it doesn’t need to explain the shorthand negated conditions in tail comments. Note also the +2 instead of -2 in the body of the loop.

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

Sidebar

Related Questions

see code, question after. #include <iostream> #include <cstdio> bool prime(unsigned long long num); int
C Code: #include <stdio.h> int main() { unsigned guess; /* current guess for prime
here is the code (java): class prime { public static boolean prime (int a,
int x; printf(hello %n World\n, &x); printf(%d\n, x);
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
int main(void) { std::string foo(foo); } My understanding is that the above code uses
I made a program that returns the sum of all primes under 2 million.
Question is: Find the sum of all the primes below 2 million. I pretty
The following program calculates all primes for really large numbers (eg. 600,851,475,143). Everything works
cout<< Name\t <<Cat\t <<Barcode\t <<Price\t <<Manufa\t <<Stock\t <<Sold\t <<ExDate\t <<Disc<<endl; for (unsigned int i=0;

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.