Professional Documents
Culture Documents
LinhLinhLinh ExercisesAndProject2
LinhLinhLinh ExercisesAndProject2
Definition:
A ship is the largest rectangle area consisting of ‘1’ cells. From a ‘1’ cell, we can go up, down,
left, right to reach another ‘1’ cell. In this exercise, we apply these rules to count the number of
ships in a given matrix.
Design:
1.Read the SHIPS.INP file.
‘1’ denotes the ship and ‘0’ denotes the sea
2.Define the ship:
If reaching a ‘1’ cell, we can move up, down, left, right to find the largest rectangle area
that can be made
3.Counting the ships
For converting the given matrix in SHIPS.INP file to a matrix in the program, I follow these
steps:
+ Iterate each line of the text file meanwhile increase the number of lines (start at 0)
+ Using function strip() to eliminate the “\n” at the end of each line, iterating each
elements in the line and typecasting them to integer, store in a list
+ Add this list to the list A to get a matrix
str.strip([chars])
str: the string that is processed
char: is an optional parameter that specifies the characters you want to remove
from the string. If not provided, whitespace characters are moved
In this exercise, strip() is used to remove the “\n” at the end of each line
Algorithm count_ships(ships):
num_ships = 0
dx=[-1,1,0,0] # the change in row if go up, down, left, right
dy=[0,0,-1,1] # the change in column if go up, down, left, right
function Built(x, y):
A[x][y]=0
for k in range(4): # there are 4 directions that we can go from a ‘1’ cell:
x1=x+dx[k] # move the row
y1=y+dy[k] # move the column
if A[x1][y1]==1:
Built(x1 , y1)
return num_ships
Program:
1. dx=[-1,1,0,0] # way to move the row
3. def Built(i,j,n,m):
6. i1 = i+dx[k]
8. if (i1>=0 and i1<n and j1>=0 and j1<m and a[i1][j1]==1): #check if the cell is in
9. Built(i1,j1,n,m) # backtracking
11. a=[]
12. n=0 # counting the rows
15. row=[int(i) for i in line.strip()] # eliminate ‘\n’ in line and store each elements in line in
a list
17. num_ships=0
21. if a[i][j]==1:
24. print(num_ships)
2. Twin Primes
Each pair of primes (p-2, p) is called twin primes.
For example, there are five twin primes below 20:
(2,3), (3,5), (5,7), (11,13), (17,19)
List all pairs of primes below 10000.
Definition:
A twin prime is a prime number that is either 2 less or 2 more than another prime number. In
other words, a twin prime is a prime that has a prime gap of two.
Design:
1. Write the function to check prime
2. Use the property of twin prime to write generate function: (p-2,p)
Algorithm :
1.Prime function
from math import *
def snt(n):
if n<2:
return 0
for i in range ( 2, isqrt(n)+1):
if n%i==0:
return 0
return 1
3. Use a list to store twin primes
4. Browse a for loop begin by “start” and finish by “end-1”
5. Use the property of function twin primes to check and add satisfied pairs to list and
print list.
Program:
FRAC.IN OUTPUT
P
1/2 2
2/3
3/4
1 / 12
Definition:
The given text file FRAC.INP consists of fractions. In this exercise we have to calculate the sum
of all these fractions
Design:
1. Read the FRAC.INP file
2. Remove the blank space in each line to collect the fraction
3. Calculate sum of all the collected fractions
To remove the blank space to get the right form of fraction, I use function replace()
str.replace(“ old ” , “ new ”, count)
str: the string that is processed
old: the string that is replaced
new: the new string that replaces the removed old string
count: is an optional parameter that specifies the maximum number of
replacements. If not provided, all the “old” strings will be replaced
Algorithm:
A=[]
for line in FRAC.INP:
A.append(line.replace(“ ”, “”))
print(sum(Fraction(i) for i in A))
Program:
1. from math import* # to use function sum()
5. for i in Frac:
6. A.append(i.replace(“ ” , “”)) # remove the blank space and add the new string to A
7. print(sum(Fraction(i) for i in A)) # change all the elements in A to fractions and calculate
the sum
4. Last zeros
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800. So 10! is ending by two zeros. How many zero ending
of 100000!
Definition:
In mathematics, trailing zeros are a sequence of 0 in the decimal representation (or more
generally, in any positional representation) of a number, after which no other digits follow. In
this exercise, we count how many zero ending of 100000!
This product is commonly known as the factorial of 100000, written 100000!
The number of zeros is determined by how many times 10=2×5 occurs in the prime
factorisation of 1000!.
There are plenty of factors of 2 in it, so the number of zeros is limited by the number of factors
of 5 in it.
These numbers have at least one factor 5:
5,10,15,20,25,...,100000 which is 100000 // 5 = 20000 numbers.
These numbers have at least two factors 5:
25,50,75,100,...,100000 which is 100000 // 25 = 4000 numbers.
These numbers have at least three factors 5:
125,250,375,500,...,100000 which is 100000 // 125 = 800 numbers
These numbers have at least four factors 5:
625,1250,1875,2500,...,100000 which is 100000 // 625 = 160 numbers
These numbers have at least five factors 5:
3125,6250,9375,12500,...,100000 which is 100000 // 3125 = 32 numbers
These numbers have at least six factors 5:
15625,31250,46875,…,100000 which is 100000 // 15625 = 6 numbers
This number has seven factors 5:
78125 which is 1 number.
So the total number of factors 5 in 100000! is:
20000+4000+800+160+32+6+1=24999
Hence there are 24999 zeros at the end of 100000!
Design:
1.Initialize a variable count = 0 to keep track of trailing zeros.
2.Iterate a loop over all the powers of 5 until the power exceeds n:
3.Divide n by the current power of 5 and add the quotient to count.
4.Return count as the final answer.
Algorithm:
def findTrailingZeros(n):
# Negative Number Edge Case
if(n < 0):
return -1
# Initialize result
count = 0
https://en.wikipedia.org/wiki/Twin_prime (3/3/2024)
https://en.wikipedia.org/wiki/Trailing_zero (5/3/2024)
https://www.geeksforgeeks.org/count-trailing-zeroes-factorial-number/ (5/3/2024)
https://www.purplemath.com/modules/factzero.htm (2/3/2024)
Chat GPT
Conclusion
Để hoàn thành được bài tiểu luận này, em xin chân thành cảm ơn thầy Nguyễn Xuân Huy,người đã tận
tình giúp đỡ và hướng dẫn bọn em thực hiện bài tập lớn này bằng tất cả lòng nhiệt tình và sự quan tâm
sâu sắc. Cảm ơn đến sự hợp tác của các thành viên trong nhóm đã giúp cho bài tập lớn được hoàn thành
một cách tốt nhất có thể.
Trong quá trình thực hiện bài tập này, do hiểu biết còn nhiều hạn chế nên bài làm khó tránh khỏi những
thiếu sót. Chúng em rất mong nhận được những lời góp ý của thầy để ngày càng hoàn thiện hơn những
kiến thức về ngôn ngữ lập trình Python.