Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Base code: Exercise 1. (30 points) Prime factorization. Finding prime factors for large integers is a difficult problem. However, doing so for small integers is

image text in transcribed

Base code:

image text in transcribed

Exercise 1. (30 points) Prime factorization. Finding prime factors for large integers is a difficult problem. However, doing so for small integers is not hard. In this assignment, you will implement a function factor integer () that finds all prime factors of a given an unsigned integer n>1. The prototype of the function is: unsigned int * factor_integer (unsigned int n, unsigned int *nf); factor_integer() must find all prime factors of n and store them in a dynamically allocated array in non-decreasing order. It then returns the starting address of the array. The number of prime factors in the array is stored in an integer pointed to by nf. The product of all elements in the factor array should be exactly n. factor_integer() should return NULL if n is 0 or 1. For example, if n is 20, the returned array has three elements: 2, 2, 5, and *nf is 3. If n is 7, the returned array has one element: 7, and *nf is 1. Here are some sample sessions of running the program. You can use a calculator to check that the number is indeed the product of all reported prime factors. $./factor 64 64=2 * 2 * 2 * 2 * 2 * 2 $./factor 192 192=2 * 2 * 2 * 2 * 2 * 2 * 3 $./factor 123456789 123456789=3 * 3 * 3607 * 3803 $./factor 1234567890 1234567890=2 * 3 * 3 * 5 * 3607 * 3803 ./factor 1999999973 1999999973=1999999973 ./factor 2000000000 2000OONNNN=? * 2 * 2 * 2 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 1 #include 2 #include 3 #include 4 5 #define MAX_INPUT 2000000000u 6 6 7 unsigned int * factor_integer (unsigned int n, unsigned int *nf) 8-{ 9 // TODO 10 11 12} 13 14 15 /*======: 16 /* print an integer */ 17 /* Do not change this function 18 /*============================ */ 19 void print_integer (unsigned int * pf, unsigned int nf) 20 - { 24 * 21 unsigned int i; 22 23 - if (pf == NULL) { printf("1"); 25 return; 26 } 27 28 - for (i = 0; i ", argv[@]); 62 return -1; 63 } 64 65 if (n MAX_INPUT ) { 66 printf("Integer n must be > 1 and 1. The prototype of the function is: unsigned int * factor_integer (unsigned int n, unsigned int *nf); factor_integer() must find all prime factors of n and store them in a dynamically allocated array in non-decreasing order. It then returns the starting address of the array. The number of prime factors in the array is stored in an integer pointed to by nf. The product of all elements in the factor array should be exactly n. factor_integer() should return NULL if n is 0 or 1. For example, if n is 20, the returned array has three elements: 2, 2, 5, and *nf is 3. If n is 7, the returned array has one element: 7, and *nf is 1. Here are some sample sessions of running the program. You can use a calculator to check that the number is indeed the product of all reported prime factors. $./factor 64 64=2 * 2 * 2 * 2 * 2 * 2 $./factor 192 192=2 * 2 * 2 * 2 * 2 * 2 * 3 $./factor 123456789 123456789=3 * 3 * 3607 * 3803 $./factor 1234567890 1234567890=2 * 3 * 3 * 5 * 3607 * 3803 ./factor 1999999973 1999999973=1999999973 ./factor 2000000000 2000OONNNN=? * 2 * 2 * 2 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 1 #include 2 #include 3 #include 4 5 #define MAX_INPUT 2000000000u 6 6 7 unsigned int * factor_integer (unsigned int n, unsigned int *nf) 8-{ 9 // TODO 10 11 12} 13 14 15 /*======: 16 /* print an integer */ 17 /* Do not change this function 18 /*============================ */ 19 void print_integer (unsigned int * pf, unsigned int nf) 20 - { 24 * 21 unsigned int i; 22 23 - if (pf == NULL) { printf("1"); 25 return; 26 } 27 28 - for (i = 0; i ", argv[@]); 62 return -1; 63 } 64 65 if (n MAX_INPUT ) { 66 printf("Integer n must be > 1 and

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions

Question

What is the growth rate of GDP per capita?

Answered: 1 week ago