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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:52:40+00:00 2026-06-14T11:52:40+00:00

I am writing a program which contains a function that checks if a number

  • 0

I am writing a program which contains a function that checks if a number is prime— if it is, it adds it to a specific list, then uses the pickle module to save it as a file. When the function is called again, it opens the list (using pickle), then checks if the number is in the list. If it is, it is a prime, if not, it checks if it is a prime using a pretty basic technique.

Here is my code:

'''
List of functions:
_isPrime(n)_ : checks if n is a prime number
_remove(n)_ : removes n from the running list of primes and updates the pkl file
_prevPrimes(n)_ : generates a list of primes up to n
_view()_ : imports and prints pList
_delete()_ : deletes all of pList
'''
def isPrime(n):
    import pickle
    from math import sqrt
    pList = pickle.load(open('primes.pkl', 'rb'))
    x=2
    if (type(n) != int) and (type(n) != long):
        print "N is not an integer."
        return False
    if n in pList:
       print "%d is a prime number." % (n)
       return True
    else:
        while (sqrt(n) >= x):
            if ((n%x) != 0):
               x = x + 1
               if (sqrt(n) < x):
                   pList.append(n)
                   pList = sorted(pList)
                   pickle.dump(pList, open('primes.pkl', 'wb'))
                   print "%d is a prime number." % (n)
                   return True
            if ((n%x)==0):
                print "%d is not a prime number." % (n)
                return False
    pList = sorted(pList)
    pickle.dump(pList, open('primes.pkl', 'wb'))

# NEW FUNCTION

def prevPrimes(n):
    from time import clock
    startTime= clock()
    import pickle
    from math import sqrt
    pList = pickle.load(open('primes.pkl', 'rb'))
    z = abs((max(pList)) - n)
    y= max(pList)
    if (z==0):
        print "Done"
    while (y <= n):
        pList = pickle.load(open('primes.pkl', 'rb'))
        if isPrime(y):
           if y not in pList:
               pList.append(y)
        y= y + 1
        pList = sorted(pList)
        pickle.dump(pList, open('primes.pkl', 'wb'))
    print startTime

# NEW FUNCTION

def remove(n):
    import pickle
    pList = pickle.load(open('primes.pkl', 'rb'))
    view()
    pList.remove(n)
    pickle.dump(pList, open('primes.pkl', 'wb'))
    view()

# NEW FUNCTION

def view():
    import pickle
    pList = pickle.load(open('primes.pkl', 'rb'))
    print pList

# NEW FUNCTION

def delete():
    import pickle
    pList = [2, 3, 5]
    pickle.dump(pList, open('primes.pkl', 'wb'))
    view()

It works fine in the Python shell.

The error that occurs is if the function actually calls. I accomplished this by doing from primenum import isPrime. However, then it gets an error with the I/O (with pickle)… Here’s a picture:

Function Error 2

As you can see, the primes.pkl file is clearly there.

How do I fix this? Thanks in advance for any suggestions 🙂

  • 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-14T11:52:41+00:00Added an answer on June 14, 2026 at 11:52 am

    You already discovered how to import your function correctly:

    from primenum import isPrime

    or refer to the full name:

    import primenum
    primenum.isPrime(5)
    

    You need to use a full path to store your pickle file; otherwise python looks for it in the local directory only. Perhaps store it in your home directory:

    import os.path
    
    HOME_DIR = os.path.expanduser('~')
    

    then later open the file with:

    open(os.path.join(HOME_DIR, 'primes.pkl')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I am writing installation script for the program which contains executable file
I'm writing a program which will allow to load a specific managed .DLL file
I am writing a Fortran program which involves a very large number of exponential
I have an huge array which contains a struct Tile. The program im writing
I am writing a program in which I use the list webservice of sharepoint
I am writing a program that counts the number of words in a text
I'm writing a program that creates an object which holds to inputs from the
I'm writing a program that uses OOP to store student records. At the moment
I'm writing a program that contains a generational garbage collector. There are just two
I'm writing a program in C++/Qt which contains a graph file parser. I use

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.