You are on page 1of 19

Page 1 of 19

Python programs
Part – 3: Spirals

By
Nidhi Chopra

(nidhi.chopra@gmail.com)
Page 2 of 19

Preface

Python programs in this eBook merge geometry and


coding. Its inspiration came from mathematics. Python
has features and ability to make it possible & look good in
GUI.

Copyright

© Nidhi Chopra
Page 3 of 19

Index

S.No. Topic Page number


1 Introduction to Python 4
2 Basic Commands 5
3 Program 1: Ulam Spiral 6-9
4 Program 2: Euler Spiral 10-11
5 Program 3: Scribble 12-13
6 Exercise 1 13-16
7 References 17
8 About author 18
9 Coming soon 19
Page 4 of 19

Introduction to Python

Python is a high-level, general-purpose programming


language. It supports multiple programming paradigms,
including structured (particularly procedural), object-
oriented and functional programming. It is often
described as a "batteries included" language due to its
comprehensive standard library.
Page 5 of 19

Basic Commands

S.No. Command Abbreviation Output Examples

1 FORWARD fd Moves turtle forward for fd(100)


number of times
specified

2 BACK bk Moves turtle back for bk(100)


number of times
specified

3 RIGHT rt Turns turtle right for rt(90)


number of degrees
specified

4 LEFT lt Turns turtle left for lt(90)


number of degrees
specified

5 PENUP penup Sets the turtle to move penup()


without drawing

6 PENDOWN pendown Resets to a drawing pen pendown()


when ordered to move

7 CIRCLE circle Makes a circle or radius, circle(r)


r
Page 6 of 19

Program 1: Ulam Spiral


Page 7 of 19

Code 1: Ulam Spiral

import turtle
t = turtle.Turtle()
t.speed(0)

scale = 4
n=1
t.lt(180)

def prime(p):
flag = 1
if p == 1:
flag = -1 #skip 1, neither prime nor composite
print("1 is neither prime, nor composite")
if p >= 2:
for q in range (2, p):
if p % q == 0:
if p != q:
Page 8 of 19

flag = 0 # composite
print(p, "is composite")
break
if flag == 1:
t.circle(1) #for every i
print(p, " is prime")

for i in range(1, 1000):


for j in range(1, i):
#print(n)
prime(n)
n=n+1
t.penup()
t.fd(scale)
t.pendown()
t.lt(90)
for j in range(1, i):
#print(n)
prime(n)
Page 9 of 19

n=n+1
t.penup()
t.fd(scale)
t.pendown()
t.lt(90)
Page 10 of 19

Program 2: Euler or Cornu Spiral


Page 11 of 19

Code 2: Euler or Cornu Spiral

import turtle

t = turtle.Turtle()
sc = turtle.Screen()
sc.setup(1500,950)
t.speed(0)
scale = 40

for i in range(1000):
t.fd(1*scale)
t.lt(i)
Page 12 of 19

Program 3: Scribble
Page 13 of 19

Code 3: Scribble

# scribble / sun / sphere

import turtle

t = turtle.Turtle()
sc = turtle.Screen()
sc.setup(1500,950)
t.speed(0)
scale = 40
for i in range(215):
t.fd(i)
t.lt(i)
print(i)
Page 14 of 19

Exercise 1: Write a python code to draw the following 3


patterns (variations of the Ulam Spiral):

Diagonal of perfect squares (in ‘red’)


Page 15 of 19

Morphed prime spiral (all the prime numbers in ‘red’


will fall on the diagonal of all the odd numbers)
Page 16 of 19

Octagonal prime spiral (all the prime numbers will fall


on the diagonal of all the odd numbers)
Page 17 of 19

References:

1. Curve Stitching: The Art of Sewing Beautiful


Mathematical Patterns by John Millington, Tarquin,
1989
2. Wikipedia
3. Various academic (python programming language)
pages and channels on YouTube
Page 18 of 19

About author

Author, Nidhi Chopra is a writer and content creator.


1. Profile: https://www.linkedin.com/in/nidhic6/
2. Scribd:
https://www.scribd.com/user/383559092/Nidhi-
Chopra
3. YouTube channel:
https://www.youtube.com/watch?v=lGAeWP5UpRA
&list=PLo_zz3TwrM7EgNyBBNDsO5xWoRSS9vtN7
4. Facebook page:
https://www.facebook.com/media/set/?set=a.28827
60398664929&type=3
Page 19 of 19

Coming soon

Python programs – part 3

You might also like