<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Classes</id>
	<title>Example code - Classes - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Classes"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Example_code_-_Classes&amp;action=history"/>
	<updated>2026-05-18T22:22:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Example_code_-_Classes&amp;diff=99&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot;You know how I often use primes in my examples, so I decided to make a prime number generator class as an example.&lt;br&gt; It is able to generate a range of primes, somewhat similar to the way &#039;&#039;&#039;range()&#039;&#039;&#039; works.&lt;br&gt; Wanting to be smart, I decided to store the primes I have already computed in a class variable, so I do not have to compute them again at least while the program runs.&lt;br&gt; I am doing a running incremental calculation of primes, storing them as I find them, and...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Example_code_-_Classes&amp;diff=99&amp;oldid=prev"/>
		<updated>2026-01-27T08:32:51Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;You know how I often use primes in my examples, so I decided to make a prime number generator class as an example.&amp;lt;br&amp;gt; It is able to generate a range of primes, somewhat similar to the way &amp;#039;&amp;#039;&amp;#039;range()&amp;#039;&amp;#039;&amp;#039; works.&amp;lt;br&amp;gt; Wanting to be smart, I decided to store the primes I have already computed in a class variable, so I do not have to compute them again at least while the program runs.&amp;lt;br&amp;gt; I am doing a running incremental calculation of primes, storing them as I find them, and...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;You know how I often use primes in my examples, so I decided to make a prime number generator class as an example.&amp;lt;br&amp;gt;&lt;br /&gt;
It is able to generate a range of primes, somewhat similar to the way &amp;#039;&amp;#039;&amp;#039;range()&amp;#039;&amp;#039;&amp;#039; works.&amp;lt;br&amp;gt;&lt;br /&gt;
Wanting to be smart, I decided to store the primes I have already computed in a class variable, so I do not have to compute them again at least while the program runs.&amp;lt;br&amp;gt;&lt;br /&gt;
I am doing a running incremental calculation of primes, storing them as I find them, and using them in my computation for the next prime. This is probably the most efficient naive implementation of finding primes. Better solutions require mathematical theories about primes - not going there. All I work with is the fact that primes are only divisible by 1 and the prime itself. Anything else in this code is deduction and inference.&amp;lt;p&amp;gt;&lt;br /&gt;
This is a type of performance improvement called caching, i.e. remembering your results, so they can be reused.&amp;lt;br&amp;gt;&lt;br /&gt;
It is possible to do something similar when computing the factorial - using a dict to store the results. The key observation to see this is possible, is that if you calculated 5!, you have also calculated 4!, 3!, 2!, 1! on the way. You might as well save these results, so you do not have to compute them again.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
# Prime number generator&lt;br /&gt;
&lt;br /&gt;
class PrimeGenerator:&lt;br /&gt;
    # Class varible, known primes in consecutive order, can be extended, but must contain these&lt;br /&gt;
    knownprimes = [2, 3]&lt;br /&gt;
    # Highest tested number for prime&lt;br /&gt;
    highesttested = 3&lt;br /&gt;
&lt;br /&gt;
    # Instatiation&lt;br /&gt;
    def __init__(self, number=None):&lt;br /&gt;
        if number is not None:&lt;br /&gt;
            # Don&amp;#039;t catch an exception below - it does want we want.&lt;br /&gt;
            number = int(number)&lt;br /&gt;
        self.target = number                &lt;br /&gt;
    &lt;br /&gt;
    # Initializing iteration&lt;br /&gt;
    def __iter__(self):&lt;br /&gt;
        if self.target is None:&lt;br /&gt;
            raise ValueError(&amp;quot;No number specified&amp;quot;)&lt;br /&gt;
        self.pos = 0&lt;br /&gt;
        return self&lt;br /&gt;
    &lt;br /&gt;
    # Find next prime&lt;br /&gt;
    def __next__(self):&lt;br /&gt;
        # Can we use the list of known primes to find the next?&lt;br /&gt;
        if self.pos &amp;lt; len(self.knownprimes):&lt;br /&gt;
            nextprime = self.knownprimes[self.pos]&lt;br /&gt;
            if nextprime &amp;gt;= self.target:&lt;br /&gt;
                raise StopIteration&lt;br /&gt;
            self.pos += 1&lt;br /&gt;
            return nextprime&lt;br /&gt;
        # No, start computing the next prime&lt;br /&gt;
        while self.target &amp;gt; PrimeGenerator.highesttested+1:&lt;br /&gt;
            PrimeGenerator.highesttested += 1&lt;br /&gt;
            if self._isprime(PrimeGenerator.highesttested):&lt;br /&gt;
                self.knownprimes.append(PrimeGenerator.highesttested)&lt;br /&gt;
                self.pos += 1&lt;br /&gt;
                return self.highesttested&lt;br /&gt;
        raise StopIteration&lt;br /&gt;
&lt;br /&gt;
    # Private method for identifying a prime&lt;br /&gt;
    def _isprime(self, number):&lt;br /&gt;
        factor = 0&lt;br /&gt;
        pos = 0&lt;br /&gt;
        while factor*factor &amp;lt;= number:&lt;br /&gt;
            # find next potential factor either in known primes or odd numbers above last known prime&lt;br /&gt;
            if pos &amp;lt; len(self.knownprimes):&lt;br /&gt;
                factor = self.knownprimes[pos]&lt;br /&gt;
                pos += 1&lt;br /&gt;
            else:&lt;br /&gt;
                factor += 2&lt;br /&gt;
            # test if it truly is a factor&lt;br /&gt;
            if number % factor == 0:&lt;br /&gt;
                return False&lt;br /&gt;
        return True&lt;br /&gt;
&lt;br /&gt;
    # It is nice be able to ask if an number is a prime&lt;br /&gt;
    def isprime(self, number=None):&lt;br /&gt;
        if number is None:&lt;br /&gt;
            number = self.target&lt;br /&gt;
        if number is None:&lt;br /&gt;
            raise ValueError(&amp;quot;Number expected&amp;quot;)&lt;br /&gt;
        if number in PrimeGenerator.knownprimes:&lt;br /&gt;
            return True&lt;br /&gt;
        if number &amp;lt;= PrimeGenerator.highesttested:&lt;br /&gt;
            return False&lt;br /&gt;
        return self._isprime(number)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    # Small testing&lt;br /&gt;
    for i in PrimeGenerator(1000):&lt;br /&gt;
        print(i)&lt;br /&gt;
&lt;br /&gt;
    print(PrimeGenerator().isprime(1000000016531)) # True&lt;br /&gt;
    print(PrimeGenerator().isprime(1000000016521)) # False&lt;br /&gt;
&lt;br /&gt;
    # Big prime, don&amp;#039;t try&lt;br /&gt;
    # 999296950101072104250052714631&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>