## Blog Pages

### Generating prime numbers using the sieve of eratosthenes algorithm

def sieve_of_eratosthenes(n):

primes = [True] * (n + 1)

primes[0] = primes[1] = False

for i in range(2, int(n ** 0.5) + 1):

if primes[i]:

for j in range(i ** 2, n + 1, i):

primes[j] = False

return [i for i in range(2, n + 1) if primes[i]]

# Example usage

n = 100

primes = sieve_of_eratosthenes(n)

print("The prime numbers up to", n, "are:", primes)

### Sierpinski triangle fractal using turtle graphics

import turtle

def sierpinski(length, depth):

if depth == 0:

for i in range(3):

turtle.forward(length)

turtle.left(120)

else:

sierpinski(length / 2, depth - 1)

turtle.forward(length / 2)

sierpinski(length / 2, depth - 1)

turtle.backward(length / 2)

turtle.left(60)

turtle.forward(length / 2)

turtle.right(60)

sierpinski(length / 2, depth - 1)

turtle.left(60)

turtle.backward(length / 2)

turtle.right(60)

turtle.speed(0)

turtle.hideturtle()

turtle.penup()

turtle.goto(-200, -200)

turtle.pendown()

sierpinski(400, 5)

turtle.exitonclick()

### Pong game

import turtle

sc = turtle.Screen()

sc.title("Pong game")

sc.bgcolor("white")

sc.setup(width=1000, height=600)

hit_ball = turtle.Turtle()

hit_ball.speed(40)

hit_ball.shape("circle")

hit_ball.color("blue")

hit_ball.penup()

hit_ball.goto(0, 0)

hit_ball.dx = 5

hit_ball.dy = -5

left_player = 0

right_player = 0

sketch = turtle.Turtle()

sketch.speed(0)

sketch.color("blue")

sketch.penup()

sketch.hideturtle()

sketch.goto(0, 260)

sketch.write("Left_player : 0 Right_player: 0",

align="center", font=("Courier", 24, "normal"))

y += 20

y -= 20

y += 20

y -= 20

sc.listen()

while True:

sc.update()

hit_ball.setx(hit_ball.xcor()+hit_ball.dx)

hit_ball.sety(hit_ball.ycor()+hit_ball.dy)

if hit_ball.ycor() > 280:

hit_ball.sety(280)

hit_ball.dy *= -1

if hit_ball.ycor() < -280:

hit_ball.sety(-280)

hit_ball.dy *= -1

if hit_ball.xcor() > 500:

hit_ball.goto(0, 0)

hit_ball.dy *= -1

left_player += 1

sketch.clear()

sketch.write("Left_player : {} Right_player: {}".format(

left_player, right_player), align="center",

font=("Courier", 24, "normal"))

if hit_ball.xcor() < -500:

hit_ball.goto(0, 0)

hit_ball.dy *= -1

right_player += 1

sketch.clear()

sketch.write("Left_player : {} Right_player: {}".format(

left_player, right_player), align="center",

font=("Courier", 24, "normal"))

if (hit_ball.xcor() > 360 and hit_ball.xcor() < 370) and (hit_ball.ycor() < right_pad.ycor()+40 and hit_ball.ycor() > right_pad.ycor()-40):

hit_ball.setx(360)

hit_ball.dx*=-1

if (hit_ball.xcor()<-360 and hit_ball.xcor() >- 370) and (hit_ball.ycor() < left_pad.ycor()+40 and hit_ball.ycor() > left_pad.ycor()-40):

hit_ball.setx(-360)

hit_ball.dx*=-1

### Brick Game

import pygame

pygame.init()

size = (600, 600)

display = pygame.display.set_mode(size)

pygame.display.set_caption("Brick Game")

floor = pygame.Rect(100, 550, 200, 10)

brick = pygame.Rect(50, 250, 10, 10)

score = 0

move = [1, 1]

continueGame = True

GREEN  = (28, 252, 106)

WHITE = (255, 255, 255)

BLACK = (0,0,0)

PINK = (252, 3, 152)

ORANGE= (252, 170, 28)

RED = (255, 0, 0)

b1 = [pygame.Rect(1 + i * 100, 60, 98, 38) for i in range(6)]

b2 = [pygame.Rect(1 + i * 100, 100, 98, 38) for i in range(6)]

b3 = [pygame.Rect(1 + i * 100, 140, 98, 38) for i in range(6)]

def draw_brick(bricks):

for i in bricks:

pygame.draw.rect(display, ORANGE, i)

while continueGame:

for event in pygame.event.get():

if event.type == pygame.QUIT:

continueGame = False

display.fill(BLACK)

pygame.draw.rect(display, PINK, floor)

font = pygame.font.Font(None, 34)

text = font.render("CURRENT SCORE: " + str(score), 1, WHITE)

display.blit(text, (180, 10))

# floor move

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_RIGHT:

if floor.x < 540:

floor.x = floor.x + 3

if event.key == pygame.K_LEFT:

if floor.x > 0:

floor.x = floor.x - 3

draw_brick(b1)

draw_brick(b2)

draw_brick(b3)

brick.x = brick.x + move[0]

brick.y = brick.y + move[1]

if brick.x > 590 or brick.x < 0:

move[0] = -move[0]

if brick.y <= 3:

move[1] = -move[1]

if floor.collidepoint(brick.x, brick.y):

move[1] = -move[1]

if brick.y >= 590:

font = pygame.font.Font(None, 74)

text = font.render("Game Over!", 1, RED)

display.blit(text, (150, 300))

font = pygame.font.Font(None, 50)

text = font.render("YOUR FINAL SCORE: " + str(score), 1, GREEN)

display.blit(text, (100, 350))

pygame.display.flip()

pygame.time.wait(5000)

break;

pygame.draw.rect(display, WHITE, brick)

for i in b1:

if i.collidepoint(brick.x, brick.y):

b1.remove(i)

move[0] = -move[0]

move[1] = -move[1]

score = score + 1

for i in b2:

if i.collidepoint(brick.x, brick.y):

b2.remove(i)

move[0] = -move[0]

move[1] = -move[1]

score = score + 1

for i in b3:

if i.collidepoint(brick.x, brick.y):

b3.remove(i)

move[0] = -move[0]

move[1] = -move[1]

score = score + 1

if score == 18:

font = pygame.font.Font(None, 74)

text = font.render("YOU WON THE GAME", 1, GREEN)

display.blit(text, (150, 350))

pygame.display.flip()

pygame.time.wait(3000)

break;

pygame.time.wait(1)

pygame.display.flip()

pygame.quit

### Photo Sideshow

import tkinter as tk

from tkinter import *

from PIL import Image

from PIL import ImageTk

root=tk.Tk()

root.geometry("200x200")

img=ImageTk.PhotoImage(Image.open("photo1.png"))

img2=ImageTk.PhotoImage(Image.open("photo2.png"))

img3=ImageTk.PhotoImage(Image.open("photo3.png"))

l=Label()

l.pack()

x = 1

def move():

global x

if x == 4:

x = 1

if x == 1:

l.config(image=img)

elif x == 2:

l.config(image=img2)

elif x == 3:

l.config(image=img3)

x = x+1

root.after(2000, move)

move()

root.mainloop()