0% found this document useful (0 votes)
3 views

Cos 102 Presentation

The document outlines two programming projects related to software engineering. The first project involves a loop that prints 'CompuNile', 'Compu', 'Nile', or the number itself based on divisibility by 3 and 5, while the second project defines a function to check if a number is prime using optimized algorithms. Both projects emphasize structure, functionality, and efficiency in their implementations.

Uploaded by

abbasarif809
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cos 102 Presentation

The document outlines two programming projects related to software engineering. The first project involves a loop that prints 'CompuNile', 'Compu', 'Nile', or the number itself based on divisibility by 3 and 5, while the second project defines a function to check if a number is prime using optimized algorithms. Both projects emphasize structure, functionality, and efficiency in their implementations.

Uploaded by

abbasarif809
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COS 102 GROUP 3

PROJECT
DEPARTMENT: SOFTWARE ENGINEERING
FACULTY: COMPUTING
Q1
For item in range(1, 101):
if item % 3 == 0 and item % 5 == 0:
print(“CompuNile”)
elif item % 3 == 0:
print(“Compu”)
elif item % 5 == 0:
print(“Nile”)
else:
• print(item)
EXPLANATION

You can explain its structure, functionality, and efficiency:

1. Structure: The code uses a loop to iterate through numbers from 1 to 100 using the `range()` function. For each number, it checks if
it’s divisible by both 3 and 5, only by 3, only by 5, or neither.

2. Functionality:
- If the number is divisible by both 3 and 5, it prints “CompuNile”.
- If the number is only divisible by 3, it prints “Compu”.
- If the number is only divisible by 5, it prints “Nile”.
- If the number is not divisible by either 3 or 5, it prints the number itself.

3. Efficiency:
Q2
• def is_prime(n):
if n <= 1:
return False
elif n <= 3:
return True
elif n % 2 == 0 or n % 3 == 0:
return False
i=5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True

# Test cases
num = int(input("Enter a number to check if it's prime: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
EXPLANATION
• you can discuss its design, functionality, and efficiency:

1. Design:
- The code defines a function `is_prime(n)` that takes an integer `n` as input and returns `True` if `n` is a prime number, and
`False` otherwise.
- It uses a common optimization technique to quickly eliminate non-prime numbers by checking divisibility by 2 and 3
separately.

2. Functionality:
- If `n` is less than or equal to 1, it returns `False` since prime numbers must be greater than 1.
- If `n` is less than or equal to 3, it returns `True` since 2 and 3 are prime numbers.
- If `n` is divisible by 2 or 3, it returns `False` since prime numbers cannot be divisible by numbers other than 1 and themselves.
- It then iterates from `i = 5` onwards in steps of 6, efficiently checking divisibility by potential factors of `n` up to the square
root of `n`.
- If `n` is divisible by any number in the form of `i` or `i + 2`, it returns `False`, indicating that `n` is not a prime number.
Otherwise, it returns `True`.

3. Efficiency:
- The code uses several optimizations to efficiently determine whether `n` is a prime number, such as checking divisibility by 2
and 3 separately, and iterating through potential factors in steps of 6.
- It avoids unnecessary calculations by stopping the loop when `i * i > n`, as any potential factor greater than the square root of
`n` will have been already checked.
- This approach makes the code efficient in terms of time complexity, particularly for larger numbers.

In summary, the code provides a fast and efficient method to determine whether a given number is prime, making it suitable for
various applications where prime number detection is required.

You might also like