Home > Net >  Prime factorization programm doesn't work for certain inputs
Prime factorization programm doesn't work for certain inputs

Time:10-29

I'm a beginner. I am suppossed to write a simple primal factorization programm and what i came up with has a weird behavior. Inputs are suppossede to be in the range of 64 bits integer(long long).

It works just fine until i input values of certain length i.e. 13. (picture attached), in which case it just shut's down without an error, which i assume indicated that the programm sees the number as 0, because it should give an error otherwise.

Now, i think problem may be in pointers or in scanf function, so i'd much appreciate if somebody points me in the way of where my mistake is. I programm in VS on Windows 10, using standard command prompt as terminal.

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<stdint.h>

int ReadInput(int64_t *n, int *ret);
void Primal_Factorization(int64_t n);

enum {INPUT_ERROR = 100, SUCCESS = 0};

int main()
{
    int ret = SUCCESS;
    int64_t n = 0;
    while (ReadInput(&n, &ret) > 0)
    {
        Primal_Factorization(n);
    }
    if (n < 0)
    {
        fprintf(stderr, "Error: Chybny vstup!\n");
        ret = INPUT_ERROR;
    }
    return ret;
}

int ReadInput(int64_t *n, int *ret){
    if(scanf("%lld", n) != 1){
        *n = 0;
        fprintf(stderr, "Error: Chybny vstup!\n");
        *ret = INPUT_ERROR;
    }
    return *n;
}

void Primal_Factorization(int64_t n){
    int64_t n_sqrt = sqrt(n);
    int count;
    int64_t n_origin = n;
    bool first_iteration = true;
    printf("Prvociselny rozklad cisla %lld je:\n", n);
    for (int i = 2; i <= n_sqrt; i  ){
        count = 0;
        if(n % i == 0){
            if(first_iteration == false) printf(" x ");
            while (n % i == 0){
                n = n / i;
                count  ;
            }
            if(count != 1) printf("%d^%d", i, count);
            else printf("%d", i);
            first_iteration = false;
        } else continue;
    }
    if(n_origin == n) printf("%lld\n", n);
    else if(n != 1) printf(" x %lld\n", n);
    else printf("\n");
}

Inputs - outputs

CodePudding user response:

In this function:

int ReadInput(int64_t *n, int *ret){
    if(scanf("%lld", n) != 1){
        *n = 0;
        fprintf(stderr, "Error: Chybny vstup!\n");
        *ret = INPUT_ERROR;
    }
    return *n;
}

The 64-bit integer is returned as an int which is then used in the main() function to check if the value is positive. The range of int is system-dependent, but if the int is 32 bit (very commonly the case), then for example 10^18 will after truncation to 32-bits result in the value -1486618624, i.e. a negative value. Thus the program terminates because the returned value from ReadInput() is negative, but it does not print the error because n (the untruncated 64-bit integer) is positive.

A minimal modification would be to have ReadInput() return a int64_t instead of int:

int64_t ReadInput(int64_t *n, int *ret)
  • Related