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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:29:44+00:00 2026-06-01T20:29:44+00:00

I was trying to solve a problem on SPOJ. We are required to calculate

  • 0

I was trying to solve a problem on SPOJ. We are required to calculate the nth twin prime pair( primes differing by 2). n can be as large as 10^5. I tried a precalculation using a sieve, I had to sieve up to 10^8 to get the maximum n twin prime, but the time limit is strict(2s) and it times out. I noticed people have solved it in 0.00 seconds, so i looked around for a formula on google, and couldnt get anything helpful. Could someone please guide me?

Thanks in advance!!

  • 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-01T20:29:46+00:00Added an answer on June 1, 2026 at 8:29 pm

    So basically, sieving up to 20,000,000 is enough, according to Wolfram Alpha. Use plain sieve of Eratosthenes, on odds, with vector<bool> in C++ (what language were you using BTW?).

    Track the twin primes right inside the sieve loop. Store the lower prime of a pair in a separate vector as you find the twins, and if an out-of-order (smaller then previous) index is requested (and they are, contrary to the examples shown on the description page), just get the prime from this storage:

    size_t n = 10000000, itop=2236;
    vector<bool> s;
    vector<int> twins;
    s.resize(n, true);
    int cnt, k1, k2, p1=3, p2, k=0;
    cin >> cnt;
    if( cnt-- > 0 )
    {
        cin >> k1;
        for( size_t i=1; i < n; ++i )  // p=2i+1
        {
            if( s[i] )
            {
                p2 = 2*i+1;
                if( p2-p1 == 2 ) { ++k; twins.push_back(p1); }
                if( k==k1 )
                { 
                    cout << p1 << " " << p2 << endl;
                    ......
    

    etc. Got accept with 1.05 sec (0.18 sec on Ideone). Or untangle the logic – just pre-calculate 100,000 twin prime pairs right away, and access them in a separate loop afterwards (0.94 sec).

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

Sidebar

Related Questions

edit: I was trying to solve a spoj problem. Here is the link to
I am trying to solve the problem Secret Code on SPOJ , and it's
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
I'm trying to solve a problem I've got on my homework. How can I
I am trying to solve this problem from SPOJ, it's a dynamic programming problem,
I've been trying to solve this problem for over an hour now, and can't
I was trying to solve a problem that required the maximum value of a
I am trying to solve SPOJ problem 5: find the next largest integer palindrome
I'm trying to solve Problem 13 of project euler, which involves the sum of
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):

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.