Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

Project: COMPUTATION - CALCULATING N-ARY VALUES (cnv0)

Objective

To create a program that can calculate and determine the number of factor pairs (nary value) over a given range of numbers.

Reading

In "The C Book", please read through Chapter 6.

Background

In mathematics, you have likely encountered the notion of “prime” numbers, those values which are divisible only by 1 and the number itself.

Expanding our view on the situation, when considering factors of a number, we have the presence of a “factor pair”; ie a pair of two values that are evenly divisible into that number.

For 17, a prime number, we have just ONE factor pair: 1 and 17:

All other values (2-16) when we divide them into 17 results in a non-zero value for the remainder.

In this way, prime, or primary, numbers, have exactly ONE factor pair. To further simplify matters, we can call it an N-ary(1) or nary(1) value. Where the number indicates the number of factor pairs.

A secondary, or nary(2) number, on the other hand, has exactly TWO sets of factor pairs.

Take the number 6, for instance:

Where 17 was a primary number, 6 is a secondary number.

Determining factor pairs

We are going to be exploring a basic, brute force, method of determining factors for a number, and that is the “trial by division” method.

Here, we successively divide a number by potential factors, to see if the factor evenly divides into the number. For convenience, we will assume the 1 and number factor pair, because EVERY number is evenly divisible by 1 and itself.

So, the number 5:

No other evenly divisible factors were found in the range 2-(N-1), therefore we are only left with the factor pair of 1 and N, making 5 an nary(1) value.

The number 14:

Because factor pairs ALWAYS come in a set of 2, we have the factor pairs of 1 and 14, along with 2 and 7.

How about 12:

There are 4 additional factors discovered here, giving us a total of 6 factors, or three factor pairs:

Notice also how the factors are nested: 1 and 12 are the outermost, 2 and 6 are encapsulated within that, and inside there, 3 and 4.

Because there are 3 factor pairs, 12 would be considered an nary(3) value (or a tertiary number).

Program

It is your task to write a program that, upon accepting various pieces of input from the user, computes the number of factor pairs of a given number or range of numbers, displaying to STDOUT all the numbers in that range that qualify as that N-ary value.

Specifications

Your program should:

Some additional points of consideration:

Execution

Primary number (nary(1)) output

lab46:~/src/cprog/cnv0$ ./cnv0 1 8 24
11 13 17 19 23 
lab46:~/src/cprog/cnv0$ 

Secondary number (nary(2)) output

lab46:~/src/cprog/cnv0$ ./cnv0 2 3 12
4 6 8 9 10 
lab46:~/src/cprog/cnv0$ 

Tertiary number (nary(3)) output

lab46:~/src/cprog/cnv0$ ./cnv0 3 11 37
12, 16, 18, 20, 28, 32 
lab46:~/src/cprog/cnv0$ 

The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.

Compiling

As we have been doing all along, use the following options to gcc when compiling:

lab46:~/src/cprog/cnv0$ gcc -Wall --std=gnu99 -o cnv0 cnv0.c
lab46:~/src/cprog/cnv0$ 

Reference

In the CPROG public directory, inside the cnv0 subdirectory, will be a copy of my implementation (in executable form, by the name ref_cnv0), which abides by the project specifications. Please compare its output against that of your implementation.

lab46:~/src/cprog/cnv0$ /var/public/spring2020/cprog/cnv0/ref_cnv0 2 2 40
4 6 8 9 10 14 15 21 22 25 26 27 33 34 35 38 39
lab46:~/src/cprog/cnv0$ 

Verification

In addition, I have also placed a cnv0verify script in that same subdirectory, which will test your program against a range of values, to determine overall correctness.

lab46:~/src/cprog/cnv0$ /var/public/spring2020/cprog/cnv0/cnv0verify
ERROR CHECK
=================
invalid nary (0): ERROR: invalid nary value (0)!
     exit status: 1, should be: 1
- - - - - - -
invalid nary (17): ERROR: invalid nary value (17)!
     exit status: 1, should be: 1
- - - - - - -
invalid lower (1): ERROR: invalid lower bound (1)!
     exit status: 2, should be: 2
- - - - - - -
invalid lower (43100): ERROR: invalid lower bound (43100)!
     exit status: 2, should be: 2
- - - - - - -
invalid upper (0): ERROR: invalid upper bound (0)!
     exit status: 3, should be: 3
- - - - - - -
invalid upper (65501): ERROR: invalid upper bound (65501)!
     exit status: 3, should be: 3
- - - - - - -
lower (300) bigger than upper (65): ERROR: lower bound (300) is larger than upper bound (65)!
     exit status: 4, should be: 4
- - - - - - -
Press ENTER to continue verification tests
nary( 1)
========
   have: >23 29 31 <
   need: >23 29 31 <

nary( 2)
========
   have: >21 22 25 26 27 33 34 35 <
   need: >21 22 25 26 27 33 34 35 <

nary( 3)
========
   have: >20 28 32 <
   need: >20 28 32 <

nary( 4)
========
   have: >24 30 <
   need: >24 30 <

nary( 5)
========
   have: >36 <
   need: >36 <

lab46:~/src/cprog/cnv0$ 

Submission

To successfully complete this project, the following criteria must be met:

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog cnv0 cnv0.c
Submitting cprog project "cnv0":
    -> cnv0.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

What I'll be looking for:

78:cnv0:final tally of results (78/78)
*:cnv0:proper error checking and status reporting performed [13/13]
*:cnv0:correct variable types and name lengths used [13/13]
*:cnv0:proper output formatting per specifications [13/13]
*:cnv0:runtime tests of submitted program succeed [13/13]
*:cnv0:no negative compiler messages for program [13/13]
*:cnv0:code is pushed to lab46 repository [13/13]

Additionally: