Glouwie Mae Nachon Exercise 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Glouwie Mae Nachon

Exercise 1.
Code:

Class ArmstrongNumberExample{

Public static void main(String[] args) {

Int c = 0, a, temp;

Int n = 153; //It is the number to check armstrong

Temp = n;

While(n > 0)

A = n % 10;

N = n / 10;

C = c +(a * a * a);

If(temp == c)

System.out.println(“armstrong number”);

Else

System.out.println(“Not armstrong number”);

Pseudocode:

1.Initialize c to zero.

2.Initialize n to a random number to check Armstrong.

3.Initialize temp to n.
Repeat steps until the value of n are greater than zero.

4.Find a reminder of n by using n%10.

5.Remove the last digit from the number by using n/10.

6.Find the thrice of the reminder and add it to c.

7.If temp == c

Print “Armstrong number”

Else

Not an Armstrong number”

Exercise 1.

2.)search on web the meaning of the following:

Integer-An integer is colloquially defined as a number that can be written without a fractional
component. For example, 21, 4, 0, and −2048 are integers, while 9.75, 5+1/2, and √2 are not.

Floating-point number- In computing, floating-point arithmetic is arithmetic using formulaic


representation of real numbers as an approximation to support a trade-off between range and
precision.

Character-Sometimes abbreviated as char, a character is a single visual object used to represent text,
numbers, or symbols. For example, the letter “A” is a single character. With a computer, one character is
equal to one byte, which is 8 bits.

String-is a data type used in programming, such as an integer and floating point unit, but is used to
represent text rather than numbers. It is comprised of a set of characters that can also contain spaces
and numbers. For example, the word “hamburger” and the phrase “I ate 3 hamburgers” are both strings.
Boolean- is a logical data type that can have only the values true or false . For example, in JavaScript,
Boolean conditionals are often used to decide which sections of code to execute (such as in if
statements) or repeat (such as in for loops).

Big theta- is either the exact performance value of the algorithm, or a useful range between narrow
upper and lower bounds. Some examples: “The delivery will be there within your lifetime.” (big-O,
upper-bound) “I can pay you at least one dollar.” (big-omega, lower bound)

Big oh- is a mathematical notation that describes the limiting behavior of a function when the argument
tends towards a particular value or infinity.

Big omega-Similar to big O notation, big Omega(Ω) function is used in computer science to describe the
performance or complexity of an algorithm. If a running time is Ω(f(n)), then for large enough n, the
running time is at least k⋅f(n) for some constant k.

A.Explain the functionality below recursive functions.

1.Total numbers of stars printed is equal to 1 + 2 + …. (n-2) + (n-1) + n, which is n(n+1)/2.

2.For a positive n, fun2(n) prints the values of n, 2n, 4n, 8n … while the value is smaller than LIMIT. After
printing values in increasing order, it prints same numbers again in reverse order. For example fun2(100)
prints 100, 200, 400, 800, 800, 400, 200, 100.

If n is negative, the function is returned immediately.

B.Give 5 examples of backtracking. Explain.

Backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution


incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the
problem at any point of time .

1.Backtracking Algorithm for Knight’s tour


Following is the Backtracking algorithm for Knight’s tour problem. Following are implementations for
Knight’s tour problem. It prints one of the possible solutions in 2D matrix form. Basically, the output is a
2D 8*8 matrix with numbers from 0 to 63 and these numbers show steps made by Knight.

2. N Queen as another example problem that can be solved using Backtracking.

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens
attack each other. For example, following is a solution for 4 Queen problem.

Backtracking Algorithm

The idea is to place queens one by one in different columns, starting from the leftmost column. When
we place a queen in a column, we check for clashes with already placed queens. In the current column, if
we find a row for which there is no clash, we mark this row and column as part of the solution. If we do
not find such a row due to clashes then we backtrack and return false.

3. Rat in a mage backtracking

A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e.,
maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source
and has to reach the destination. The rat can move only in two directions: forward and down. In the
maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from
source to destination. Note that this is a simple version of the typical Maze problem. For example, a
more complex version can be that the rat can move in 4 directions and a more complex version can be
with a limited number of moves.

Backtracking Algorithm: Backtracking is an algorithmic-technique for solving problems recursively by


trying to build a solution incrementally. Solving one piece at a time, and removing those solutions that
fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time
elapsed till reaching any level of the search tree) is the process of backtracking.

Approach: Form a recursive function, which will follow a path and check if the path reaches the
destination or not. If the path does not reach the destination then backtrack and try other paths.

Algorithm:

Create a solution matrix, initially filled with 0’s.


Create a recursive function, which takes initial matrix, output matrix and position of rat (I, j).

If the position is out of the matrix or the position is not valid then return.

Mark the position output[i][j] as 1 and check if the current position is destination or not. If destination is
reached print the output matrix and return.

Recursively call for position (i+1, j) and (I, j+1).

Unmark position (I, j), i.e output[i][j] = 0.

4.subset sum backtracking

Subset sum problem is to find subset of elements that are selected from a given set whose sum adds up
to a given number K. We are considering the set contains non-negative values. It is assumed that the
input set is unique (no duplicates are presented).

Exhaustive Search Algorithm for Subset Sum

One way to find subsets that sum to K is to consider all possible subsets. A power set contains all those
subsets generated from a given set. The size of such a power set is 2N.Backtracking Algorithm for Subset
Sum

Using exhaustive search we consider all subsets irrespective of whether they satisfy given constraints or
not. Backtracking can be used to make a systematic consideration of the elements to be selected.

Assume given set of 4 elements, say w[1] … w[4]. Tree diagrams can be used to design backtracking
algorithms. The following tree diagram depicts approach of generating variable sized tuple.

5.m Coloring Problem

Given an undirected graph and a number m, determine if the graph can be coloured with at most m
colours such that no two adjacent vertices of the graph are colored with the same color. Here coloring of
a graph means the assignment of colors to all vertices.

Naïve Approach: Generate all possible configurations of colors. Since each node can be coloured using
any of the m available colours, the total number of colour configurations possible are m^V.

After generating a configuration of colour, check if the adjacent vertices have the same colour or not. If
the conditions are met, print the combination and break the loop.

Algorithm:

1.Create a recursive function that takes current index, number of vertices and output colorarray
2.If the current index is equal to number of vertices. Check if the output color configuration is safe, i.e
check if the adjacent vertices do not have same color. If the conditions are met, print the configuration
and break.

Give Queues problems and solutions:

1.MAGNETIC BASE STANCHIONS

Magnetic base posts help create a semi-permanent but flexible barrier that is designed specifically for
high-traffic venues that call out for a sturdy stanchion that won’t shift out of place. The magnet firmly
attaches to a floor-mounted steel plate, keeping the stanchion in place until you’re ready to move it for
cleaning or rearranging the queue.

Solution:Compromised queue integrity When your queue perimeter or partition wall is continually
challenged to maintain its integrity as customer traffic flows through, you need a strong base. You want
to avoid core-drilling stanchions into the floor but seek the integrity and strength of a permanent
solution.

2.KNOW YOUR “COUNTS.”

Footfall analytics solutions are a queue manager’s best friend. A variety of people-counting technologies
are available to assist, ranging from sensors to WiFi tracking. Find the best solution for your particular
business, and get busy counting.

Solution:Wait times in check Tracking how many customers are in and around the queuing area, and
also tracking current average wait times, estimated future wait times, and abandonment rates and
customer traffic patterns can give you valuable insight to keep wait times in check, conversions high, and
customers satisfied. But you can’t just do this by eyeballing the queue.

You might also like