Em python, a estrutura básica de sequências de dados é chamada de lista. O programa abaixo explora um pouco essa estrutura:
Python 2
# Keep all the prime numbers from 1 to 100
import numpy as np
def is_prime(n):
prime=True
count = 2
while(prime and count<=np.floor(np.sqrt(n))):
print count
if(np.mod(n,count)==0):
prime=False
count=count+1
if(prime):
return True
else:
return False
if __name__ == '__main__':
primes=[]
is_prime(49)
n=100
for i in range(2,n+1):
if(is_prime(i)):
primes.append(i)
print primes # All the primes
print primes[9] # The 9th prime number
#I want to show all the prime numbers
for prime in primes:
print prime
#I want to multiply the 10 first primes
multiplePrime=1
for i in range(10):
multiplePrime=multiplePrime*primes[i]
print multiplePrime
print primes[2:5] # Slices of the list
Python 3
import numpy as np
def is_prime(n):
prime=True
count = 2
while(prime and count<=np.floor(np.sqrt(n))):
print (count)
if(np.mod(n,count)==0):
prime=False
count=count+1
if(prime):
return True
else:
return False
if __name__ == '__main__':
primes=[]
n=100
for i in range(2,n+1):
if(is_prime(i)):
primes.append(i)
print (primes) # All the primes
print (primes[9]) # The 9th prime number
#I want to show all the prime numbers
for prime in primes:
print (prime)
#I want to multiply the 10 first primes
multiplePrime=1
for i in range(10):
multiplePrime=multiplePrime*primes[i]
print (multiplePrime)
print (primes[2:5]) # Slices of the list