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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:03:57+00:00 2026-05-30T20:03:57+00:00

I’ve tried to compare my result to Wolfram Alpha’s result by counting primes found.

  • 0

I’ve tried to compare my result to Wolfram Alpha’s result by counting primes found.

It seems works well, without error.

But when I submit this solution to SPOJ, it shows the error message “wrong answer”.

I also tried to change the final print’s end= to ” (blank string), but still got “wrong answer”.

Not sure if is there something wrong with my sieve algorithm, or output error.

**edit: link of problem http://www.spoj.pl/problems/PRIME1/

Here is my PRIME1 source code, hope someone can point out my fault. Thanks a lot.

And hope someone can teach me how to write a test to programs like this, I’m still learning, don’t know how to do automated tests to programs, but want to learn.

    def getPrimeInRange(minNum, maxNum):
        #just a variation with skipping step of the  Sieve of E's
        processingRange = list(range(minNum, maxNum+1))
        #prefix, due to 1 is not a prime
        if minNum == 1:
            processingRange[0] = 0
        sqrtOfMaxNum = int(maxNum ** 0.5) + 1
        primesUnderSqrt = list(range(sqrtOfMaxNum))
        #prefix, due to 1 is not a prime
        primesUnderSqrt[1] = 0

        #my strategy is flip all numbers that is not a prime to zero, which equals to False.
        #for Sieve of E's, all the primes under sqrt of max num are the only needed numbers to sieve primes out.
        #so here is a smaller Sieve of E's for numbers under sqrt
        for n in primesUnderSqrt:
            if n: #which equals to "if n != 0"
                nowIndex = n + n
                while True:
                    try:
                        primesUnderSqrt[nowIndex] = 0
                        nowIndex += n
                    except IndexError:
                        break

        #full aspect sieve
        for n in primesUnderSqrt:
            if n:
                #for easier flip processing, I need the offset number for the flipping.
                nMultipleMostCloseToMin = n * (minNum // n)
                if nMultipleMostCloseToMin == minNum:
                    nowIndex = 0
                elif sqrtOfMaxNum <= minNum:
                    nowIndex = nMultipleMostCloseToMin + n - minNum
                elif sqrtOfMaxNum > minNum:
                    nowIndex = nMultipleMostCloseToMin + n - minNum + n

                #happy flippin'
                while True:
                    try:
                        processingRange[nowIndex] = 0
                        nowIndex += n
                    except IndexError:
                        break

        return processingRange

    def main():
        todoTimes = int(input())
        todoNums = list(range(todoTimes))
        stringOutput = ''
        for i in range(todoTimes):
            todoNums[i] = input().split()
            todoNums[i][0] = int(todoNums[i][0])
            todoNums[i][1] = int(todoNums[i][1])
        for [minNum, maxNum] in todoNums:
            #countedNum = 0 #for algo debugging
            for n in getPrimeInRange(minNum, maxNum):
                if n:
                    stringOutput += str(n)
                    #countedNum += 1 #for algo debugging
                    stringOutput += '\n'
            stringOutput += '\n'
            #stringOutput += str(countedNum) #for algo debugging
        stringOutput = stringOutput.rstrip('\n')
        print(stringOutput)


    ifMainSucceed = main()
  • 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-05-30T20:03:58+00:00Added an answer on May 30, 2026 at 8:03 pm

    This part of your logic

    if nMultipleMostCloseToMin == minNum:
        nowIndex = 0
    elif sqrtOfMaxNum <= minNum:
        nowIndex = nMultipleMostCloseToMin + n - minNum
    elif sqrtOfMaxNum > minNum:
        nowIndex = nMultipleMostCloseToMin + n - minNum + n
    

    is wrong. Your elif-conditions don’t make much sense here. If n is not a divisor of minNum, the smallest multiple of n not less than minNum is nMultipleMostCloseToMin + n, regardless of whether sqrtOfMaxNum is larger than minNum or not. The condition you intended here was n <= minNum, to avoid crossing off the prime itself.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but

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.