1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need you eyes. Is this significant? Ulam spiral variation.

  1. Mar 19, 2012 #1
    I've been meddling with prime numbers a bit with my programming studies and managed to make an ulam spiral using python. While trying to find a faster sieve to more efficiently generate bigger spirals I made a small error which produced this pattern:
    It didn't seem noteworthy at first as I've made a couple of other images other than spirals. For example when you draw the integers in a stack instead of a spiral, i.e. ten number across on the first line then ten numbers across on the second and so on, with different widths you find different patterns similar to this one, most of which are simply due to the fact of distribution of evens. But even without the evens, some still produce less noticeable patterns for example certain sequences completely void of primes even though they are all odd numbers. But since the image produced here is a spiral, it's a little harder for me to understand the pattern and where it comes from, also whether it is any thing meaningful.

    How it happened:
    I use the sieve of Eratosthenes and in an attempt to remove all even numbers which is pretty straight forward I my mistake was that broke that age old law of programming which is not to iterate over the same list you are changing. So I had a list of integers from 0 to n with all non-primes replaced with zero's and all primes left as they were. Then since all even numbers would obviously be zero in this list I tried to remove them entirely to shorten the size of the list. I also accidently started from 1 instead of zero or 2 which means I accidently attempted to remove all odd numbers(it was late, still is). So with the sieve applied and all non-primes as zero, starting from 1 you delete that index value in the list and move up in steps of two, except that since your iterating over a changing list the next number wont be three but 5 since 1 plus 2 is 3 which is the index value for 5 as the list indexes start from 0. This is becoming confusing so let me just post the code and if you happen to know a bit of programming or even sudo code then it should be understandable.
    Code (Text):

    def sieve(a):
        plist = range(0,a+1)
        prime = 2
        inc = 4
        while prime <= math.sqrt(a):
            while True:
                    plist[inc] = 0
                except IndexError:
                inc += prime
            while True:
                prime += 1
                if plist[prime] != 0:
                    inc = prime * 2
        i = 1
        while True:
                i += 2
            except IndexError:
        return plist
    The main part is the last few lines or the last while loop.
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted