Python Forum

Full Version: 10x10 Array of Prime Numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to construct a 10 by 10 array of prime numbers. I am able to construct an array of prime numbers but do not understand how to restrict the array to a 10 by 10.

Python Code:

import numpy as np
import math

def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

a = np.arange(1, 10**3),
foo = np.vectorize(is_prime)
pbools = foo(a)
primes = np.extract(pbools, a)
x = [[foo for i in range(10)] for j in range(10)]
print(primes)
Any help would be greatly appreciated
Use the reshape method of the array, with the dimensions you want as a tuple.