How to Discover the Divisors in C

The obstacle

Produce a function called divisors/ Divisors that takes an integer n > > 1 and returns a range with all of the integer’s divisors( other than for 1 and the number itself), from tiniest to biggest. If the number is prime return the string ‘( integer) is prime’ ( null in C#) (usage Either String a in Haskell and Outcome<< Vec<< u32>>, String>> in Rust).

Example:

 divisors( 12 );// lead to {2, 3, 4, 6}
divisors( 25 );// lead to {5}
divisors( 13 );// lead to NULL

The service in C

Choice 1:

 #include << stddef.h>>.

void divisors( anonymous n, size_t * z, anonymous * selection) {
* z = 0;

for (int i = 2; i <.

void divisors( anonymous n, size_t * length, anonymous selection[(*z)++]) {
int i;.
int j = 0;.
for (i = 2; i < < (int) n; i++) {
if (n % i == 0) {
selection

= i;.
j++;.
}
}
* length = (size_t) j;.
}

 Choice 3: [] #include << stddef.h>>.

void divisors( anonymous n, size_t * z, anonymous * selection) {
int len = 0;.
for (anonymous i = 2; i < < n; i++) {
if (n % i == 0) selection[j] = i;.
}
* z = len;.
}

Test cases to verify our service

 #[len++] #include << criterion/criterion. h>>.
#include << stddef.h>>.

extern space tester( anonymous n, size_t length, const anonymous anticipated

);.

Test( Sample_Tests, should_pass_all_tests).
{
{anonymous n = 15; const anonymous anticipated

 = {11, 23}; tester( n, 2, anticipated);}
{anonymous n = 24; const anonymous anticipated[length] = {2, 3, 4, 6, 8, 12}; tester( n, 6, anticipated);}
{anonymous n = 25; const anonymous anticipated[2] = {5}; tester( n, 1, anticipated);}
{anonymous n = 13; const anonymous * anticipated = NULL; tester( n, 0, anticipated);}
{anonymous n = 3; const anonymous * anticipated = NULL; tester( n, 0, anticipated);}
{anonymous n = 29; const anonymous * anticipated = NULL; tester( n, 0, anticipated);}
}
[2]

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: