I'm working on implementing a parallel algorithm of the Sieve Of Erastothenes in C using BSP.
My code compiles and executes but doesn't print the primes.
When I do ./bspsieve 2 1000 for example the only output I get is
"It took : 0.000371 seconds for proc 0 out of 2." while it should be printing all the found primes!
Oddly enough the algorithm does work it seems like. If I use a larger upperbound in the above example, it takes longer. When I assign more processors, it takes less time. So probably a stupid mistake somewhere, but I have serious problems with C and am working on a remote computer so unsure of that tools I've got...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mcbsp.h>
/*
Note: To compile, this file has to be in the same folder as mcbsp.h and you need the 2 following commands:
gcc -Iinclude/ -pthread -c -o bspsieve.o bspsieve.c
gcc -o bspsieve bspsieve.o lib/libmcbsp1.1.0.a -lpthread -lrt
*/
int procs;
int upperbound;
int *primes;
//SPMD function
void bspSieve(){
bsp_begin(procs);
int p = bsp_nprocs(); // p = number of procs obtained
int s = bsp_pid(); // s = proc number
float blocksize; // block size to be used, note last proc has a different size!
if( s != p-1){
blocksize = ceil(upperbound/p);
} else {
blocksize = upperbound - (p-1)*ceil(upperbound/p);
}
// Initialize start time and end time, set start time to now.
double start_time,end_time;
start_time = bsp_time();
// Create vector that has block of candidates
int *blockvector;
blockvector = (int *)malloc(blocksize*sizeof(int));
int q;
for(q = 0; q<blocksize; q++){
//List contains the integers from s*blocksize till blocksize + s*blocksize
blockvector[q] = q + s*blocksize;
}
//We neglect the first 2 'primes' in processor 0.
if(s == 0){
blockvector[0] = 0;
blockvector[1] = 0;
}
// We are using the block distribution. We assume that n is large enough to
// assure that n/p is larger than sqrt(n). This means that we will always find the
// sieving prime in the first block, and so have to broadcast from the first
// processor to the others.
long sieving_prime;
int i;
bsp_push_reg( &sieving_prime,sizeof(long) );
bsp_sync();
for(i = 2; i * i < upperbound; i++) {
//Part 1: if first processor, get the newest sieving prime, broadcast. Search for newest prime starting from i.
if(s == 0){
int findPrimeNb;
for(findPrimeNb = i; findPrimeNb < blocksize; findPrimeNb++) {
if( blockvector[findPrimeNb] != 0) {
sieving_prime = blockvector[findPrimeNb];
//broadcast
int procNb;
for(procNb = 0; procNb < p; ++procNb){
bsp_put(procNb, &sieving_prime,&sieving_prime,0,sizeof(long));
}
break;
}
}
}
bsp_sync();
//Part 2: Sieve using the sieving prime
int sievingNb;
for(sievingNb = 0; sievingNb < blocksize; sievingNb++){
//check if element is multiple of sieving prime, if so, pcross out (put to zero)
if( blockvector[sievingNb] % sieving_prime == 0){
blockvector[sievingNb] = 0;
}
}
}
//part 3: get local primes to central area
int transferNb;
long transferPrime;
for(transferNb = 0; transferNb < blocksize; transferNb++){
transferPrime = blockvector[transferNb];
primes[transferPrime] = transferPrime;
}
// take the end time.
end_time = bsp_time();
//Print amount of taken time, only processor 0 has to do this.
if( s == 0 ){
printf("It took : %.6lf seconds for proc %d out of %d. \n", end_time-start_time, bsp_pid(), bsp_nprocs());
fflush(stdout);
}
bsp_pop_reg(&sieving_prime);
bsp_end();
}
int main(int argc, char **argv){
if(argc != 3) {
printf( "Usage: %s <proc count> <upper bound> <n", argv[ 0 ] );
exit(1);
}
//retrieve parameters
procs = atoi( argv[ 1 ] );
upperbound = atoi( argv[ 2 ] );
primes = (int *)malloc(upperbound*sizeof(int));
// init and call parallel part
bsp_init(bspSieve, argc, argv);
bspSieve();
//Print all non zeros of candidates, these are the primes.
// Primes only go to p*p <= n
int i;
for(i = 0; i*i <= upperbound; i++) {
if(primes[i] > 0) {
printf("%d, ",primes[i]);
}
}
return 0;
}
printf doesn't automatically include a trailing newline, and it doesn't generally flush the output buffer until it outputs a newline; so probably you just need to add a
printf("\n");
at the end of your program, just before your return 0;.
Alternatively, or additionally, if you want to see the output as-you-go (if BSP allows that), you can add a
fflush(stdout);
right after your printf("%d, ",primes[i]);, to explicitly flush the output buffer.
Related
Am I freeing memory correctly in this program with just free(lineArr) at the end of main()?
I still can't figure out what's causing the issue with all the chars in my output (image attached). I know it's probably something basic with how I have the for loops set up.. things print correctly the first run after compiling, but not the second run. Why would this be?
Thanks
// When the sum of proper divisors for a number is the same as the number
// itself, the "status" of that sum may be considered "perfect"; when less,
// "deficient", and when greater than, "abundant".
//
// This program takes two extra command-line arguments after the executable
// name: an integer and a character. The integer will be the number of
// numbers past 2 to print statuses and histogram bars for; the character
// will be used to construct a histogram bar with height = to the sum of
// divisors for a particular number. Example arguments: ./a.out 6 '*'
// Example output:
// 2 is Deficient *
// 3 is Deficient *
// 4 is Deficient ***
// 5 is Deficient *
// 6 is Perfect ******
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int sumOfDivisiors(int aNum); //prototype for sumOfDivisiors function
typedef struct{ //define a structure called "line" with three members
int lineNum;
int sum;
char status[10];
}line;
int main(int argc, char *argv[]){ //main will accept command line arguments
int howMany;
char usrChar;
line whichLine;
if(argc < 3){
printf("Error: must enter 3 command line arguments.\n");
exit(1);
}
sscanf(argv[1],"%d", &howMany);
sscanf(argv[2], "%c", &usrChar);
line** lineArr = malloc(howMany * sizeof(line*)); //allocate mem for array of struct ptrs
if (lineArr == NULL){
printf("Error: trouble allocating memory. Exiting.\n");
exit(1);
}
for (int n = 2; n <= howMany; n++){ //loop to call func + initialize lineNum, sum, status for current line
int sumResult = sumOfDivisiors(n);
lineArr[n] = malloc(sizeof(line)); //allocate mem for pointer to current line struct
if (lineArr[n] == NULL){
printf("Error: trouble allocating memory. Exiting.\n");
exit(1);
}
line* temp = lineArr[n];
temp->lineNum = n;
temp->sum = sumResult;
if (temp->sum == n){
strcpy(temp->status, "Perfect");
} else if (temp->sum < n){
strcpy(temp->status, "Deficient");
} else {
strcpy(temp->status, "Abundant");
}
}
for (int i = 2; i <= howMany; i++){ //loop to print formatted results
printf("%3d %-10s ", i, lineArr[i]->status);
for (int j = 0; j < lineArr[i]->sum; j++){
printf("%c", usrChar);
}
printf("\n");
}
free(lineArr); //free dynamically allocated memory
return 0;
}
//Definition for sumOfDivisiors function. This function accepts an int number
//as an argument. It takes that number, finds all proper divisors (divisors
//less than the number itself), then returns the integer result of adding
//up all these divisors.
int sumOfDivisiors(int aNum){
int result = 0;
int i;
for (i = 2; i <= sqrt(aNum); i++){
if (aNum % i == 0){
if (i == (aNum/i)){
result += i;
} else {
result += (i + aNum/i);
}
}
}
return(result + 1);
}
I'm currently learning C and have been practicing on codewars recently. I came across this question on prime gaps and was curious on how to improve it. I was initially fooled in thinking this wouldn't be as bad but I realized that finding primes is difficult (especially for large numbers where it can be at least an NP-Hard problem). I know my code right now has multiple for-loops and this is terrible in terms of performance. I also don't fully know the clean ways of writing C so there might be some no-nos I did (e.g. I know it's my responsibility to free up dynamically allocated memory but I tried freeing memory in the main() calling function and by freeing the first element of the allocated memory block--not sure if this is the appropriate way of freeing up a block of memory)
In general, the main function calls the prime_gap function several times. I know this code works because it was submitted successfully but any tips on writing this better (algorithmically in C)?
/* a prime gap of length "n" indicates that n-1 consecutive composite numbers exist between two primes.
* For example, the gap beween (2,3) is 1, the gap between (5,7) is 2 and the gap between (7,11) is 4.
* Our function should return the first pair of primes that satisfies the gap that we're looking for in a search between two numbers. /
There should also be no primes that exist within the gap of the first two primes that are found.
* gap(g, n, m) -> where g = gap length, n = start of search space, m = end of search space
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
long long *gap(int g, int n, int m);
bool check_prime(int, bool);
int main(int argc, const char *argv[]){
long long *check3 = gap(2,100,110);
for (int i = 0; i < 2; i++){
printf("%lld ", check3[i]);
}
free(&check3[0]);
printf("\n");
long long *check = gap(2,3,50);
for (int i = 0; i< 2; i++){
printf("%lld ", check[i]);
}
printf("\n");
free(&check[0]);
long long *check1 = gap(2,5,5);
for (int i = 0; i < 2; i++){
printf("%lld ", check1[i]);
}
free(&check1[0]);
printf("\n");
long long *check2 = gap(4,130,200);
for (int i = 0; i < 2; i++){
printf("%lld ", check2[i]);
}
free(&check2[0]);
printf("\n");
long long *check4 = gap(6,100,110);
for (int i = 0; i < 2; i++){
printf("%lld ", check4[i]);
}
free(&check4[0]);
printf("\n");
long long *gap(int g, int n, int m) {
long long *result = (long long*) malloc(sizeof(long long) *2); // dynamically allocate 2 long longs for the integer array
if (result == NULL){
perror("Not enough memory");
}
int test = 0;
static bool prime;
for (int i = n; i < m; i++) { // traverse search space
prime = true;
prime = check_prime(i, prime);
if (prime == true) { // identifies prime number
test = i + g; // add the gap value to identified prime
prime = false; // set bool to false to now check for any primes that exist between i and i+gap
for (int z = i+1; z < test; z++ ) { // check there is no prime in between the first and second (test) primes
prime = check_prime(z, prime);
if (prime == true) break;
}
if (prime != true) { // found no primes between i and i+gap
prime = true; // set bool to true to then toggle off in the check right below if i+gap is not actually prime
prime = check_prime(test, prime); // now need to check whether i+gap itself is a prime
if (prime == true) {
result[0] = i; result[1] = test;
return result;
}
}
}
}
result[0] = result[1] = 0;
return result;
}
bool check_prime(int i, bool prime){
for (int j = 2; j <= sqrt(i); j++){
if (i % j == 0) {
return false;
}
}
return true;
}
Reading you code, the following comments come to mind:
you are never freeing the space allocated by the malloc
therefore I am wondering if you really need to use malloc, a simple global variable would have been sufficient for what you are doing with it
you check_prime function has a second parameter prime that is never used
in function gap, the variable prime is indicated as static, this is not required, it could also lead to errors
from the algorithmic point of view:
your logic goes like
for i in range to check:
if i is prime
check if all the number between i and i+gap are not prime
if i+gap is prime then return the tuple(i, i+gap)
globally, you are checking several times for the same number if it is prime, since this is by far the most "expensive" operation, you should try not to
specifically, you should start by checking test before iterating over all the numbers in the range i..test.
My code:
#include <stdio.h>
int main()
{
int count = 0;
int size = 0;
float num[size];
int i = 0;
float avg = 0;
float sum = 0;
while (scanf("%f",&num) != EOF)
{
if ((num[i] != num[i+1]) && (num[i] != num[i-1]))
{
sum = sum + num[i];
size++;
}
}
avg = sum/size;
printf("%0.2f", avg);
}
My input and output:
//input
2
2
1
^Z
//output
1.67
Correct input and output:
2 2 1
^Z
1.50
My question:
1) How can I make my code prompt input of numbers all on one line separated with spaces in between each input? Right now, my code always starts a new line after entering a number.
2) How can I fix my code so that it only calculates the average of non-repeated numbers? (NOTE: my code has to run no slower than O(nlogn). ) So I can't use nested loops as it will then have a run time of O(n^2).
You have some problems in your code:
the usage of num
test on scanf
the test of already used number.
the usage of num
When you write
int size = 0;
float num[size];
You do not allocate memory to store numbers.
and
while (scanf("%f",&num) != EOF)
Is not correct since you are not storing the value read into a float: gcc warns:
warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘float (*)[(sizetype)(size)]’ [-Wformat=]
A more correct way to do would be to write:
float num;
...
while (scanf("%f",&num) != EOF)
test on scanf
You test that scanf does not return EOF, but what if your conversion failed if you do not give a number?
The correct way to test what the user gives, is to check that you have the number of conversion wanted:
while (scanf("%f",&num) == 1)
the test of already used number.
Writting
if ((num[i] != num[i+1]) && (num[i] != num[i-1]))
You test last number (i) against past and future number (?!)
You have a simplier approch: have an array to store the already got number. This imply to have a function to test a number has already been got.
Warning The current implementation of is_number_in_array it very naive and make your program run in O(n). You can easyly replace it with some dichotomic search which is O(log n)
So a corrected version of your code could be:
#include <stdio.h>
int is_number_in_array(float num, float *array, int size)
{
for (int i =0; i < size; ++i)
{
if (num == array[i])
return 1;
}
return 0;
}
#define MAX_NUMBER 50
int main(void)
{
/* number of number read */
int size = 0;
/* number already read */
float array[MAX_NUMBER] = {};
/* currently read number */
float num = 0;
float avg = 0;
float sum = 0;
/* get next number, stop when a conversion failed */
while (scanf("%f",&num) == 1)
{
/* test if number is already in array */
if (! is_number_in_array(num, array, size )) {
/* not in array: add it */
array[size ++] = num;
sum += num;
}
/* Add some test here to check that size is not as big as MAX_NUMBER */
}
avg = sum/size;
printf("%0.2f", avg);
return 0;
}
First, read the numbers into an array nums. Let n be the number of numbers in the array. This can be done in O(N). I leave this to you.
Secondly, sort the array using a O(N log N) algorithm. I leave this to you.
Finally, we can identify duplicates by simply consulting neighbours. The tricky part is avoiding going out of bounds. Accessing nums[-1] or nums[n] would result in Undefined Behaviour, so we have to avoid that.
We don't need to look ahead and backwards. We want to use a number the first time we encounter it, so we only need to look backwards.
Don't forget to make sure we have at least one number, because we can't divide by zero.
if (!n) {
fprintf(stderr, "Can't find the average of zero numbers.\n");
exit(1);
}
float sum = 0;
size_t uniques = 0;
for (size_t i=0; i<n; ++i) {
if (i > 0 && nums[i] == nums[i-1])
continue;
sum += nums[i];
++uniques;
}
float avg = sum/uniques;
The complexity analysis is O(N + N log N + N) = O(N log N).
(Both of the other answers include O(N^2) solutions
As an aside, we can do better than O(N log N).
In practical terms, inserting into a well-written hash table has an amortized performance of O(1), and lookups are O(1). We can use this to devise an O(N) solution to the problem.
Using Perl, since it has such hash tables built-in:
#nums
or die("Can't find the average of zero numbers.\n");
my %seen; # Hash table.
my $sum = 0;
my $uniques = 0;
for my $num (#nums) {
next if $seen{$num}++;
$sum += $num;
++$uniques;
}
my $avg = $sum/$uniques;
This approach saves all integers in an array, as long they are not saved already, as to avoid duplicates.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int bytes_read;
int size = 1000;
int i = 0;
int amount = 0;
int number = 0;
// boolean flag to detect duplicate numbers.
int duplicate = 0;
int sum = 0;
float unique_avg = 0;
// Allocate char / int arrays on the heap.
// These can be resized with realloc() if needed.
char* string = (char *) malloc (size);
int* unique_num = (int *) calloc (2 * size, 0);
// Declare char pointers to be used will splitting the string.
char* token;
char* rest = string;
printf ("Please enter a string: ");
bytes_read = getline (&string, &size, stdin);
// In case getline fails to get the input.
if (bytes_read == -1) {
puts ("error!");
}
// getline() read input succesfully.
else {
// Iterate over all space separated string tokens.
while ((token = strtok_r(rest, " ", &rest))){
// Convert string token to number
number = atoi(token);
for(i = 0; i < 2 * size; ++i){
if(number == unique_num[i]){
// Duplicate found.
duplicate = 1;
break;
}
}
if(!duplicate){
unique_num[amount] = number;
++amount;
}
// Restore value of duplicate for next iteration
duplicate = 0;
}
}
// Sum all unique numbers.
for(i = 0; i < amount; ++i){
sum += unique_num[i];
}
// Calculate the avg of unique numbers.
// Float casting is required for the fractional part of the division.
unique_avg = (float) sum / (float) (amount);
// Print the average.
printf("%f", unique_avg);
return 0;
}
Running this code in console yields with example input of 2 2 1:
Please enter a string: 2 2 1
1.500000
Adding the printf("Hi!\n") statements allows the code to work. It also works if the bound initial bound is improper and the user enters a new one. When I ran some tests calculate divers sometimes returned a character instead of an integer. I'm thinking it has something to do with my memory allocation. I also noticed that ./a.out 6 10 "|" would work but ./a.out 6 25 "|" would not causing an infinite loop when printing the lines of "|".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Structs
typedef struct data_struct {
int lineNumber;
int divisorSum;
char type[10];
}data;
// Prototypes
int calculateDivsors(int integer);
// Functions
int main (int argc, char *argv[]) {
int lowerBound;
int upperBound;
char character;
// Gets the values from command-line
sscanf(argv[1], "%d", &lowerBound);
sscanf(argv[2], "%d", &upperBound);
sscanf(argv[3], "%c", &character);
// Check to see if bound is proper
while (upperBound <= lowerBound || lowerBound < 2) {
printf("Error, please enter a new range (positive increasing).\n");
scanf("%d %d", &lowerBound, &upperBound);
}
// Structure calls
data* info = NULL;
int totalData = upperBound - lowerBound;
// Allocate the memory
info = (data*)malloc(totalData * sizeof(data));
printf("Hi!\n");
if (info != NULL) {
// Iterate through all the digits between the two bounds
for (int i = lowerBound; i <= upperBound; i++) {
int sum = calculateDivsors(i);
// Write data to indiviual structures
info[i].lineNumber = i;
info[i].divisorSum = sum;
// Check to see if the sum is greater than, less than, or equal to the original
if (sum == i) {
strcpy(info[i].type, "Perfect");
}
else if (sum > i) {
strcpy(info[i].type, "Abundant");
}
else if (sum < i) {
strcpy(info[i].type, "Deficient");
}
// Line n# has a column width of 4, string of 10
printf("%4d is %-10s\t", info[i].lineNumber, info[i].type);
// Generate Pictogram
for (int j = 0; j < info[i].divisorSum; j++) {
printf("%c", character);
}
printf("\n");
}
}
}
// Adds up the sum of diviors
int calculateDivsors(int integer) {
int sum = 0;
for (int i = 1; i < integer; i++) {
// Add to sum if perfectly i is a sum of integer
if (integer % i == 0) {
sum += i;
}
}
return sum; // Returns the sum of diviors
}
You are accessing data outside its allocated buffer whenever lowerBound doesn't start with 0.
info[i].lineNumber = i;
Ideally, you should become...
info[i - lowerBound].lineNumber = i;
To ensure that the indexing starts at 0. Further, your window between lowerBound and upperBound is inclusive. That means it includes both ending boundaries. Therefore, totalData is undersized by one element. Even if you fix the indexing problem, your code will still be wrong with this:
int totalData = (upperBound - lowerBound) + 1;
Failing to do both of the above causes your code to invoke undefined behavior (UB), and thus unpredictable results thereafter. It may even appear to work. That, however, is a red herring when your code has UB. Don't confuse defined behavior with observed behavior. You can trust the latter only once you have the former; the two are not synonymous.
I've a problem at the execution of my program.
I compile it with gcc using: gcc -std=c89 -Wall -Wextra -lm main.c.
Same problem with clang and I didn't try it with tcc yet but I will when the program will be finished.
I would like it to be compilable with a c89 compiler, this explains some of my choices. I've no error at the compilation but a problem at the execution.
Like what's written in some of my comments in my code, at the execution normally, I would have an error because of a division by 0 (with the k variable) but I think I have an error in the "do...while" loop (because the rest of my function is tested), and before the by k division.
I took some hours to debug (especially with gdb (-g option add to the compilation command and run -v in gdb) but I'm still blocked.
/*librairies*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*main function*/
int main(){
int N, b, i, j, h, k;
int *t; /*pointer which will become an array (see in another after comment)*/
float l, m, a;
k = 0; /*Problem with k because we will see later an error*/
printf("Your number : ");
scanf("%d", &N);
if (N>1){
a = sqrt(N);
b = a; /*we convert float into int and it rounds under, normal*/
t = malloc(N * sizeof(int)); /*manual dynamic memory allocation for the array to respect C89*/
if (t==NULL){ /*t is null when you have not enough ram for a too big value for N*/
exit(0);
}
for (i=1;i<N;i++){ /*function to fill the array with 2,3..N*/
j = i + 1;
h = i - 1;
t[h] = j;
}
do{
if (t[k]==0){
k = k + 1;
} else{
for (i=1;i<N;i++){ /*problem nearby, because the division by 0 error does not come know at the execution = 0*/
h = i - 1;
if (t[h]!=0){
l = (float)t[h] / (float)k; /*It's going to be a problem because in the first loop, k = 0*/
m = l - (int)l;
if (m==0.0f){
t[h] = 0;
}
}
}
}
} while (k<b);
h = 0;
for (i=1;i<N;i++){
h = i - 1;
printf("%d\n", t[h]); /*printf to test before continuing the program*/
}
} else{
printf("Your number is not valid\n");
}
return 0;
}
Info: This program is just a personnal project to illustrate for me a math course I had (about prime numbers, and for know with the problem solved and the k problem solved, it should display:
Your number: N /*N is a valid (positive integer) stdin input*/
0 /*and 0 until the k<b prime values are passed but I will make them to be displayed*/
a prime number
0
the next prime number
0
the next prime number
0
the next prime number
0
the next prime number
0
.
.
the last prime number (which can be N if N is a prime one)
The theorem used for this program is:
You choose a number
You calculate its square root
1, here is not a considered prime number because it divides all prime
numbers.
You write all the numbers from 2 to the number you choose
You remove every multiples of prime numbers (the prime numbers used here are those under the square root) every where in the written list (here an array where we write 0 because I don't know how to use structs (list?) for now).
The numbers which stay are all prime numbers under the one you chose (rest? sorry for my bad English)
Thanks.
the following proposed code:
cleanly compiles
performs the desired functionality
is NOT efficient
shows proper indentation
shows meaningful variable names
properly passes the allocated memory to `free_ before exiting
properly checks for successful execution of scanf()
and now, the proposed code:
/*librairies*/
#include <stdio.h>
#include <stdlib.h>
/*main function*/
int main( void )
{
size_t targetNum;
size_t i; // index
size_t j; // index
size_t *primeArray = NULL; /*pointer which will become an array (see in another after comment)*/
printf("Enter Your target number : ");
if( scanf("%lu", &targetNum) != 1 )
{
fprintf( stderr, "scanf failed to read target number\n" );
exit( EXIT_FAILURE );
}
if ( targetNum > 1 )
{
primeArray = malloc( targetNum * sizeof( size_t ) );
if ( !primeArray )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// initialize array of prime numbers
for ( i = 0; i < targetNum; i++ )
{
primeArray[i] = 1;
}
primeArray[0] = 0;
primeArray[1] = 0;
// find primes and zero multiples
for( i = 0; i < targetNum; i++ )
{
// skip any multiples of primes (which have already been zero'd
if( !primeArray[i] )
{
continue;
}
// zero multiples
for( j = i+i; j < targetNum; j+=i )
{
primeArray[j] = 0;
}
}
// print primes
for ( i = 0; i < targetNum; i++ )
{
if( primeArray[i] )
{
printf( "%lu\n", i );
}
}
free( primeArray );
}
else
{
printf("Your number is not valid\n");
}
return 0;
}
given the following input:
4
the output is:
Enter Your target number : 4
2
3