0% found this document useful (0 votes)
22 views20 pages

Unit-4 PPTs

The Sieve of Eratosthenes algorithm finds all prime numbers up to a given number n by starting with a list of consecutive integers from 2 to n, progressively crossing out multiples of the prime numbers found until all prime numbers are found. It uses the property that every number is either a prime or is made up of prime factors, which allows it to identify prime numbers by eliminating multiples of each prime that is found.

Uploaded by

fearless61022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

Unit-4 PPTs

The Sieve of Eratosthenes algorithm finds all prime numbers up to a given number n by starting with a list of consecutive integers from 2 to n, progressively crossing out multiples of the prime numbers found until all prime numbers are found. It uses the property that every number is either a prime or is made up of prime factors, which allows it to identify prime numbers by eliminating multiples of each prime that is found.

Uploaded by

fearless61022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Sieve of Eratosthenes algorithm and implementation

Sieve of Eratosthenes Algo.


1. Define a list of consecutive integers from 2 to given value of n: (2, 3,
4, …, n).
2. Start from first prime number 2 To find next prime
3. Move to p2 (Square of p)with increments of p thereafter and
mark/strike or delete every such element till n.
4. Find the first number greater than Previous p in the list that is not
marked/striked/deleted.This is next prime number. If there was no
such number, stop.
5. Repeat from step 3 for next prime numbers

SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Sieve of Eratosthenes

SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Final Solution in Yellow

SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Try the solution
SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
File Manipulation in python

• Python too supports file handling and allows users to handle files i.e.,
to read and write files, along with many other file handling options, to
operate on files
• You can use a file to store data permanently

SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Working of open() function
• We use open () function in Python to open a file in read or write mode. As explained above,
open ( ) will return a file object.
• The syntax being:
file_variable= open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
• “ r “, for reading.
• “ w “, for writing.
• “ a “, for appending.
• “ x “, creates specified file returns error if file already exist.

• If not passed, then Python will assume it to be “ r ” by default


• "t" - Text - Default value. Text mode

• "b" - Binary - Binary mode (e.g. images)

• The default is reading in text mode. In this mode, we get strings when reading from the file.
example
• file = open('geek.txt', 'r')
• # This will print every line one by one in the file
• for each in file:
• print (each)
Creating a file
• sales_file = open('sales.txt', 'w')

• File named sales.txt will be created


• Variable sales_file will reference a file object that we can use to write
data to the file
Specifying the Location of a File
• Suppose a current program is located in the following folder on a
C:\Users\Blake\Documents\Python
• E.g.:
test_file =open('C:\Users\Blake\temp\test.txt', 'w')
The file object atrributes:
• One we open a file. An file object is
created and we can do number of things
on it.

• Here is a list of all attributes related to file


object:

• file.closed: True if file is closed,otherwise


return false
• file.mode: Returns access mode with
which file was opened
• file.name: Return name of the file.
• Example:
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode

• This would produce following result:


Name of the file: foo.txt
Closed or not : False
Opening mode : wb
Writing Data to a File
• Once we have opened a file in file object, we can use this objects
methods to perform various operations like writing data.
• file_variable.write(string)

• If we write three names using above statement separately


• File content: name1\nname2\nname3\n
USE of Append Mode—

Example—
f = open("demofile.txt", "a")
f.write("This line is added at the end by write command")
f = open("demofile.txt", "r")
print(f.read( ))
f.close( )

USE of Write Mode—

f = open("demofile.txt", "w")
f.write("Sorry! the contents are deleted")
Delete a File
To delete a file, we must import the OS module, and run its os.remove() function:
Example
Remove the file "demofile.txt":
import os
os.remove("demofile.txt")
• Check if File exist:

• To avoid getting an error, we might want to check if the file exists before we try to delete it:

• Example

• Check if file exists, then delete it:



• import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Reading data from file
• Data can be read using open() function with read mode. Content are
read into memory as buffer and referenced the file reference object.
• infile = open(‘filename.txt', 'r')
• Data= Infile.read() will read file content as a string and will assign to
corresponding variable.
• Read(optional numeric parameter)
• If we pass a number less than total number of character in file. It will
read only those many character in the file. By default all characters
are read or we can pass -1 to read whole file.
readline()
• We can read the content of file line by line using this function
• File_variable.readline() will read one line at a time.
infile = open('philosophers.txt', 'r')
# Read three lines from the file.
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
infile.close()
Read lines separately

• Syntax is
• f = open("demofile.txt", "r")
• print(f.readlines( ))
• OUTPUT—

• ['Hello World!\n', 'This file is only for testing purpose.\n', 'File handling in Python.\n', 'All the
Best!!!’]
• (File content as list of lines)
• NOTE:
• 1. After performing any operations, we must close the file.
• f.close( )
• 2. By looping through the lines of the file, we can read the whole file, line by line:

• f = open("demofile.txt", "r")
for lines in f:
print(lines)
F.seek, F.tell
• seek() used to change the position of file handler/pointer to given
position.
Sytax: f.seek(offset)
For text mode files.
• f.tell() gives the current location of file pointer.

You might also like