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....
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).
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
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT