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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:56:26+00:00 2026-05-16T01:56:26+00:00

I am trying to solve problem 41 , but i don’t know why my

  • 0

I am trying to solve problem 41, but i don’t know why my code stops at 987654319:

def IsPandigital(No):
 a = sorted(str(No))
 for i in range(1,10):
  if a[i - 1] != str(i):
   return False
 return True
def IsPrime(No):
 i = 2
 while i < No:
  if No % i == 0:
   return False
  i += 1
 return True
i = 987654321
while i > 0:
 print i
 raw_input()
 if IsPrime(i) == True and IsPandigital(i) == True:
  print i
  break
 i -= 1
print i
print "EOP"
raw_input()

P.S: I know that i should start from 799999999 because:

GergS: Every 9 digit and 8 digit pandigital number is divisible by 3.

  • 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-16T01:56:27+00:00Added an answer on May 16, 2026 at 1:56 am

    Your IsPrime function is very slow. It quickly calculated that 987654321 is dividable by 3 and 987654320 is dividable by 2. However, 987654319 is prime and it takes very very long to check it against all divisors so it seems as if it stopped.

    The problem requires more than just simple calculation, as you did. Calculating prime numbers is a slow process, so you should optimize it, for example:

    • Test IsPandigital before IsPrime,
    • Even better, create a list of only
      pandigital numbers and make the prime
      test. Hint: the pandigital numbers are: [987654321,987654312,987654231,987654213,...]
    • You dont have to make loop while i <
      No
      – it is enough to go up to
      sqrt(No).
    • Numbers ending with digit 0, 2, 4, 5, 6 or 8 are never prime
    • Another option might be to calculate all n-digit prime numbers and then pick the largest pandigital number.

    Your other problems:

    • You are calculating the largest 9-digit pandigital prime. The problem says “the largest n-digit pandigital prime”, so you should be able to calculate any length depending on a parameter
    • You should not start from 799999999, as you wrote. You should simply skip calculations for n=3, n=5, n=6, n=8 and n=9 because none of those is prime.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.