Question

In: Computer Science

TITLE PRIME FACTORIZATION USING AN ARRAY-BASED STACK INTRODUCTION This project revisits one of the problems in...

TITLE

PRIME FACTORIZATION USING AN ARRAY-BASED STACK

INTRODUCTION

This project revisits one of the problems in Project 2, and it will re-use a function within the solution developed there.

An integer is prime if it is divisible only by itself and 1. Every integer can be written as a product of prime numbers, unique except for their order, called its prime factorization. For example,

1776 = 37 x 3 x 2 x 2 x 2 x 2.

DESCRIPTION

Design, implement, document, and test an interactive program that reads positive integers from the terminal and writes their prime factorizations to the screen. The program should list the factors of each integer in decreasing order, as in the example above, and should terminate when the user enters 0 or a negative value.

INPUT

The program's input is positive integers, entered from the terminal, terminated by 0 or a negative value.

OUTPUT

The program's output consists of prompts for the input values and the input values' prime factorizations. It writes this output to the terminal.

ERRORS

The program can assume that its input is integers; it need not detect any errors.

EXAMPLE

A session with the program might look like this:

  Enter a positive integer (0 to stop): 1776
    Prime factors: 1776 = 37 x 3 x 2 x 2 x 2 x 2

  Enter a positive integer (0 to stop): 6463
    Prime factors: 6463 = 281 x 23

  Enter a positive integer (0 to stop): 349856
    Prime factors: 349856 = 29 x 29 x 13 x 2 x 2 x 2 x 2 x 2

  Enter a positive integer (0 to stop): 36423479
    Prime factors: 36423479 = 36423479

  Enter a positive integer (0 to stop): 1
    Prime factors: 1 = 1

  Enter a positive integer (0 to stop): 0
    

OTHER REQUIREMENTS

Implement a stack abstract data type in a class in which a typedef statement specifies the type of the stack's items. Use a sequential (array-based) stack implementation. As the program identifies prime factors, it should save them by pushing them onto a stack of integers provided by this class.

If d >= 2 is the smallest integer that divides an integer n, then d is prime. Therefore the program can identify prime factors (in ascending order) by repeatedly identifying the smallest factor of the target value and then dividing the target value by the factor. The program need not test that these smallest factors are prime or attempt to list only prime values; this will happen automatically.

Thus, to identify the prime factors of n, first find the smallest factor that divides n, push it onto the stack, and divide n by the factor. Then find the smallest factor of the new value of n; handle it the same way. Continue in this way until n = 1. Then pop and print the values until the stack is empty.

This scheme identifies prime factors in order of increasing magnitude; saving the values on the stack and then popping and printing them displays them in decreasing order, as specified.

HINT

Recall that you wrote a function that returns the smallest factor of its positive integer argument back in Project 2. You can re-use that function in this project.

HAND IN

See About Programming Assignments for a description of what to hand in: design document, user document, code, tests, and summary.

Question: If we wanted to report each integer's prime factors in increasing order, would the stack be necessary or helpful? Explain.

Solutions

Expert Solution

In this question, we need to input a number from user and output its prime factors. But the factors has to outputted in descending order, ie, greatest to smallest. This can be done by using a stack, by pushing each factor into it, and at the end, popping all the factors and printing them. The complete working code for this question is:

This is the class test with some methods to calculate the prime factors:

In this picture, you can see the main/driver code and the output:


Related Solutions

In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
Problem 1. Using prime factorization, find gcd(900, 140) and lcm(900, 140).
Problem 1. Using prime factorization, find gcd(900, 140) and lcm(900, 140).
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Explain how you can solve the following problems using the QR factorization. (a) Find the vector...
Explain how you can solve the following problems using the QR factorization. (a) Find the vector x that minimizes ||Ax − b1||^2 + ||Ax − b2||^2 . The problem data are the m × n matrix A and two m-vectors b1 and b2. The matrix A has linearly independent columns. If you know several methods, give the most efficient one. (b) Find x1 and x2 that minimize ||Ax1 − b1||^2 + ||Ax2 − b2||^2 . The problem data are the...
Matlab project: Solve using Matlab three problems: One using the combination formula One using the permutation...
Matlab project: Solve using Matlab three problems: One using the combination formula One using the permutation of n objects formula One using the permutation of r objects out of n objects You can pick these problems from the textbook or you can make up your own questions.
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT