Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Backtracking Algorithm

1. The group created a backtracking algorithm to check if an entered number is prime or not. The backtracking algorithm builds solutions by searching all possibilities and backtracking on failures. 2. Their backtracking algorithm checks if an entered number x is divisible by 1 and itself or 2 and 3. If not prime, it checks the next number y with the same criteria. If y fails, it checks z. 3. They converted the backtracking prime number algorithm into a flowchart showing the steps: start, read variables, check divisibility of x, check y if x fails, check z if y fails, then stop.

Uploaded by

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

Backtracking Algorithm

1. The group created a backtracking algorithm to check if an entered number is prime or not. The backtracking algorithm builds solutions by searching all possibilities and backtracking on failures. 2. Their backtracking algorithm checks if an entered number x is divisible by 1 and itself or 2 and 3. If not prime, it checks the next number y with the same criteria. If y fails, it checks z. 3. They converted the backtracking prime number algorithm into a flowchart showing the steps: start, read variables, check divisibility of x, check y if x fails, check z if y fails, then stop.

Uploaded by

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

PROGRAMMING FUNDAMENTALS WITH

LABORATORY

GROUP 3:
POLYMORPHS

Group Activity no. 2


Leader & Members:

Cablitas, Charlyn Mae Mangayao, Laurence Marquez, John Isaac Penus, Hannah Wong, Marie Shaira Lou
(Group leader) (Member) (Member) (Member) (Member)
Are you ready?
DIRECTION:
Activities / Assessment Tasks: Do the following tasks with your groupmates. Your
answers should not be longer than one short bond paper. Prepare to be called in
class. Use Arial 12 with 1.15 line spacing.

1. Refer to this site https://www.geeksforgeeks.org/introduction-to-algorithms/ and


read the Types of Algorithms discussed. With your group, create an algorithm
corresponding to the explanation given. For example, Brute Force Algorithm is used
to find the best solution when we see a problem. Your algorithm should evolve around
a scenario where you see a potential problem then find out the best solution to solve
the problem. Note: Coordinate with the other groups to make sure that your algorithm
is not the same as theirs. One group per algorithm only.

2. Create an Algorithm that will check whether an entered number is Prime or not. A
prime number is a whole number greater than 1 whose only factors are 1 and itself.
A factor is a whole number that can be divided evenly into another number. The first
few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. Numbers that have more
than two factors are called composite numbers.

3. Convert the algorithm you created above into flowchart.


1. Backtracking Algorithm
The backtracking algorithm basically builds
the solution by searching among all
possible solutions. Using this algorithm,
we keep on building the solution following
criteria. Whenever a solution fails, we
trace back to the failure point and build on
the next solution and continue this process
till we find the solution, or all possible
solutions are looked after (What Is
Algorithm | Introduction to Algorithms -
GeeksforGeeks, 2019)
2. Using Backtracking
Algorithm, check
whether an entered
number is prime or not.
STEP 1

Step 1: Fulfilling the pre-requisites

1. The problem that is to be solved by this algorithm: check whether an


entered number is prime or not.
2. The constraints of the problem that must be considered while solving the
problem: The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be
checked.
4. The output to be expected when the problem is solved: The prime
number.
5. The solution to this problem, in the given constraints: The solution
consists of checking the three numbers. It can be done by divisibility.
STEP 2

Step 2: Designing the algorithm

Check whether an entered number is prime or not.


1. Start
2. Read variables x, y, and z
3. Check if x is divisible by 1 and itself or by 2 and 3, then display x is the prime number
else
check if y is divisible by I and itself or by 2 and 3
then display y is the prime number
else
check if 2 is divisible by 1 and itself or by 2 and 3
then display z is the prime number.
4. Stop.
3. Flowchart showing how
the backtracking
algorithm checks whether
an entered number is
prime or not.
REFERENCE

Backtracking Algorithms - GeeksforGeeks. (2017). GeeksforGeeks. Retrieved from


https://www.geeksforgeeks.org/backtracking-algorithms/
Thank you
See you next
time!

You might also like