I'm completely new to Python. I have recently read the below three points about iterators:
1. An iterator is "non-destructive", ie you can not change the data structure as it loops through the iterator itself. They can therefore be used to make data "read only". Another way of saying this is: An iterator is "one way", data can be retrieved, but not submitted.
2. An iterator has consciousness - ie, it knows and remembers where the last in the sequence was. You do not need to keep track of an index.
3. An iterator is often memory-efficient. A typical example is the huge files. Let's say you have a log file on 1GB. The code to read this file will eat up your memory. But by looping over the file object via the built-in iterator it will never use more memory than every single line.
Coming to the point: Here is the code I'm working with:
Code:
# Open the webdocument with The First 10,000 Primes, read the file and print the content. It functions for Python version 2.5, But Not for version 3.1.2
# import urllib
file = urllib . urlopen ( 'http://primes.utm.edu/lists/small/10000.txt' )
primes = file . read ()
print ( primes )
Code:
# Brute force prime number and prime number calculation twin. There ar no security net, exception
# action in the code. Note That all twin prime except (3, 5) are of the form: (nx6) + / - 1
print('This prgram is only valid for the natural number Between 1 and 1700')
print()
primed= [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101] # Python list inbuilt
import math # import math module
n = int(input ('Enter a natural number Between 1 and 1700:'))
m1 = n*6 - 1
m2 = n*6 + 1
print()
limit=int(math.ceil(math.sqrt(m2))) + 1 # Ceiling of square root of number + 1
bool1=1
for the prime in prime: # Loop through the list of known prime.
if prime <= limit:
if m1 % prime == 0: # Enough two test for prime factors up to the square root
bool1=0 # No prime
break # Break out of the loop if a prime factor is found.
if bool1 == 1:
print (m1, 'is prime')
else:
print (m1, 'is NOT prime')
print()
bool2=1
for the prime in prime: # Loop through the list of known prime.
if prime <= limit:
if m2 % prime == 0: # Enough two test for prime factors up to the square root
bool2=0 # No prime
break # Break out of the loop if a prime factor is found.
if bool2 == 1:
print (m2, 'is prime')
else:
print (m2, 'is NOT prime')
print()
print()
Enter=input('Hit enter') # Prevent output window from closing.
Problems:
- The first code works with Python 2.5, but not with Python 3.*. Maybe I'm missing some external library since Python 2.5 was installed from a third party with many external extension and a library.
- Switch the most effective method and iterate over the file object as suggested in the introduction.
- How to combine the first code and the second code so well-known prime number is read from the specified or custom website. Filter out the text in the preamble to the file on the website.
- Calculate even larger prime numbers that are saved to text/html file.
Bookmarks