Sunteți pe pagina 1din 14

AIM: TO CONSTRUCT A PYTHON PROGRAM FOR

DESGNING A KOCH CURVE (PATTERN PRINTING)

SOURCE CODE:
# Python program to print complete Koch Curve.
from turtle import *

# function to create koch snowflake or koch curve


def snowflake(lengthSide, levels):
if levels == 0:
forward(lengthSide)
return
lengthSide /= 3.0
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)
right(120)
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)

# main function
if __name__ == "__main__":
# defining the speed of the turtle
speed(0)
length = 300.0

# Pull the pen up – no drawing when moving.


# Move the turtle backward by distance, opposite
# to the direction the turtle is headed.
# Do not change the turtle’s heading.
penup()
backward(length/2.0)

# Pull the pen down – drawing when moving.


pendown()
for i in range(3):
snowflake(length, 4)
right(120)

# To control the closing windows of the turtle


mainloop()

Koch Curve or Koch Snowflake

What is Koch Curve?


The Koch snowflake (also known as the Koch curve, Koch star, or Koch island) is
a mathematical curve and one of the earliest fractal curves to have been
described. It is based on the Koch curve, which appeared in a 1904 paper titled
“On a continuous curve without tangents, constructible from elementary
geometry” by the Swedish mathematician Helge von Koch.
The progression for the area of the snowflake converges to 8/5 times the area of
the original triangle, while the progression for the snowflake’s perimeter diverges
to infinity. Consequently, the snowflake has a finite area bounded by an infinitely
long line.

Construction
Step1:
Draw an equilateral triangle. You can draw it with a compass or protractor, or just
eyeball it if you don’t want to spend too much time drawing the snowflake.
 It’s best if the length of the sides are divisible by 3, because of the nature of this
fractal. This will become clear in the next few steps.

Step2:
Divide each side in three equal parts. This is why it is handy to have the sides
divisible by three.

Step3:
Draw an equilateral triangle on each middle part. Measure the length of the
middle third to know the length of the sides of these new triangles.

Step4:
Divide each outer side into thirds. You can see the 2nd generation of triangles
covers a bit of the first. These three line segments shouldn’t be parted in three.

Step5:
Draw an equilateral triangle on each middle part.
 Note how you draw each next generation of parts that are one 3rd of the mast one.
What are fractals
A fractal is a never-ending pattern. Fractals are infinitely complex patterns that
are self-similar across different scales. They are created by repeating a simple
process over and over in an ongoing feedback loop. Driven by recursion, fractals
are images of dynamic systems – the pictures of Chaos.

What is turtle programming in python??


Turtle graphics is a popular way for introducing programming to kids. It was part
of the original Logo programming language developed by Wally Feurzig and
Seymour Papert in 1966.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle,
give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the
direction it is facing, drawing a line as it moves. Give it the command
turtle.right(25), and it rotates in-place 25 degrees clockwise.
By combining together these and similar commands, intricate shapes and
pictures can easily be drawn.
The turtle module is an extended reimplementation of the same-named module
from the Python standard distribution up to version Python 2.5.
It tries to keep the merits of the old turtle module and to be (nearly) 100%
compatible with it. This means in the first place to enable the learning
programmer to use all the commands, classes and methods interactively when
using the module from within IDLE run with the -n switch.
The turtle module provides turtle graphics primitives, in both object-oriented and
procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it
needs a version of Python installed with Tk support.
This Python Code allow you to create Snowflakes design by using its standard
library Turtle for GUI designing. This code creates 20 (you can change it in the
source code) snowflakes randomly of random size and color in random position
of the screeen.
AIM:TO CONSTRUCT A PYTHON PROGRAM FOR
DESIGNING SNOWFLAKES USING TURTLE MODULE

SOURCE CODE:
# Python code to draw snowflakes fractal.
import turtle
import random

# setup the window with a background color


wn = turtle.Screen()
wn.bgcolor("cyan")

# assign a name to your turtle


elsa = turtle.Turtle()
elsa.speed(15)

# create a list of colours


sfcolor = ["white", "blue", "purple", "grey", "magenta"]

# create a function to create different size snowflakes


def snowflake(size):

# move the pen into starting position


elsa.penup()
elsa.forward(10*size)
elsa.left(45)
elsa.pendown()
elsa.color(random.choice(sfcolor))

# draw branch 8 times to make a snowflake


for i in range(8):
branch(size)
elsa.left(45)
# create one branch of the snowflake
def branch(size):
for i in range(3):
for i in range(3):
elsa.forward(10.0*size/3)
elsa.backward(10.0*size/3)
elsa.right(45)
elsa.left(90)
elsa.backward(10.0*size/3)
elsa.left(45)
elsa.right(90)
elsa.forward(10.0*size)

# loop to create 20 different sized snowflakes


# with different starting co-ordinates
for i in range(20):
x = random.randint(-200, 200)
y = random.randint(-200, 200)
sf_size = random.randint(1, 4)
elsa.penup()
elsa.goto(x, y)
elsa.pendown()
snowflake(sf_size)

# leave the window open until you click to close


wn.exitonclick()
AIM: TO WRITE A PROGRAM TO CHECK THE GIVEN
NUMBER IS A TROJAN NUMBER OR NOT

SOURCE CODE:

# Python 3 program to check if a number


# is Trojan Number or not
from math import sqrt, pow

# Function to check if a number


# can be expressed as x^y
def isPerfectPower(n):
if (n == 1):
return True

# Try all numbers from 2 to


# sqrt(n) as base
for x in range(2, int(sqrt(n)) + 1):
y=2
p = pow(x, y)

# Keep increasing y while power


# 'p' is smaller than n.
while (p <= n and p > 0):
if (p == n):
return True
y += 1
p = pow(x, y)

return False

# Function to check if a number


# is Strong
def isStrongNumber(n):
count = {i:0 for i in range(n)}
while (n % 2 == 0):
n = int(n / 2)
count[2] += 1

# count the number for each


# prime factor
for i in range(3,int(sqrt(n)) + 1, 2):
while (n % i == 0):
n = int(n / i)
count[i] += 1

if (n > 2):
count[n] += 1

flag = 0

for ket,value in count.items():

# minimum number of prime


# divisors should be 2
if (value == 1):
flag = 1
break

if (flag == 1):
return False
else:
return True

# Function to check if a number


# is Trojan Number
def isTrojan(n):
if (isPerfectPower(n) == False and
isStrongNumber(n)):
return True
else:
return False

# Driver Code
if __name__ == '__main__':
n = 108

if (isTrojan(n)):
print("YES")
else:
print("NO")

Check if a number is a Trojan Number


Given a Number . The task is to check if N is a Trojan Number or not.
Trojan Number is a number that is a strong number but not a perfect power. A
number N is known as a strong number if, for every prime divisor or factor p of N,
p2 is also a divisor. In other words, every prime factor appears at least twice.
All Trojan numbers are strong. However, not all strong numbers are Trojan
numbers: only those that cannot be represented as m k, where m and k are
positive integers greater than 1.
Examples:
Input : N = 108
Output : YES

Input : N = 8
Output : NO
The idea is to store the count of each prime factor and check if the count is
greater than 2 then it will be a Strong Number.
This part can easily be calculated by PRIME FACTORISATION
The next step is to check if the given number cannot be expressed as x y. To
check whether a number is perfect power
:

S-ar putea să vă placă și