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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:42:13+00:00 2026-06-05T21:42:13+00:00

I have a function is_prime(n) which returns True if n is prime and False

  • 0

I have a function is_prime(n) which returns True if n is prime and False otherwise. In NumPy I am looping, checking if an array contains primes, and the start of the array will be identical through every iteration, so I want to memoize the is_prime(n) function to avoid a lot of unnecessary calculations.

Since I have an array, I want to vectorize is_prime(n) so I can apply it on arrays element by element, NumPy style. I do this with one line from the NumPy tutorial (shown later)

I also use a memoization template I found on the net:

def memoize(function):
    cache = {}
    def decorated_function(*args):
        if args in cache:
            return cache[args]
        else:
            val = function(*args)
            cache[args] = val
            return val
    return decorated_function

Then:

is_prime = memoize(is_prime)

BUT, is V_prime now correctly memoized if i now vectorize the memoized is_prime function?:

V_prime = np.vectorize(is_prime)

Thank you

  • 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-05T21:42:14+00:00Added an answer on June 5, 2026 at 9:42 pm

    Well lets test it.

    import numpy as np
    
    def test(input):
        return input
    
    def memoize(function):
        cache = {}
        def decorated_function(*args):
            if args in cache:
                print 'cached'
                return cache[args]
            else:
                print 'not cached'
                val = function(*args)
                cache[args] = val
                return val
        return decorated_function
    
    test = memoize(test)
    print test(9)
    print test(9)
    test = np.vectorize(test)
    print test(9)
    print test(10)
    print test(10)
    

    I get this on my machine.

    not cached
    9
    cached
    9
    cached
    cached
    9
    not cached
    10
    cached
    10
    

    so yes, it is memoize, on my machine using numpy 1.6.1

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

Sidebar

Related Questions

I have seen that a prime number implementation of the GetHashCode function is being
hi i have function which is called by tinker listbox so i cannot return
I have a function which calculates distance from database records. This function is called
I have a function which is described in one of my controllers. The function
I have a function which is like $(function() { var data = [ {
I have this function which is supposed to set a certain time format to
I am struggling with getJSON. I have a simple StockWatcher application which returns the
Let's say there is a library function called get_pack() which returns a Pack object:
I have a PHP highlighting function which makes certain words bold. Below is the
i have an init() function that returns a list of Domaines needed to populate

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.