1

how to find the correct type for largest number ?

#include <stdio.h>
/**
 * factor_prime - prints the prime factors of a number
 * @n: number
 */

void factor_prime(unsigned long long n)
{
    long i;
    long inter = n;

    printf("n : %lld\n", n);
    for (i  = 2; i <= n; i++)
    {
        if (n % i == 0)
        {
            n = n / i;
            printf("%ld=%lld*%ld\n", inter, n, i);
            return;
        }
    }
}

int main(void)
{
    factor_prime(1718944270642558716715u);
}

output : 3397071787570416427=35568825191561*95507

expected : 1718944270642558716715=343788854128511743343*5

how to fix ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
iamMAHAM
  • 68
  • 1
  • 7
  • Working with really big numbers (aka "bignums") isn't as easy as working with ordinary integers. Usually you need to use an external library; one popular one is [GNU MP](https://gmplib.org/). – Steve Summit Jun 26 '22 at 11:57
  • Unsigned long long is basically 8 bytes and can store values from 0 to 18,446,744,073 709,551,615. Your number is bigger, so there is the warning – elo Jun 26 '22 at 12:00
  • 1
    For starters, you should use `ull`, not `u` for an `unsigned long long`, but that's probably not enough. See previous comment or try using a `uint64_t`. Also, the algorithm you are using is way too slow for that factorization. `i` should have the same type of `n`, too. – Bob__ Jun 26 '22 at 12:00
  • By the way, 343788854128511743343 is not a prime number. The factorization I would expect is 1718944270642558716715 = 5 × 7 × 29 × 10771 × 157231561910911. – Steve Summit Jun 26 '22 at 12:00
  • In the end, though, you probably don't need to worry about making your program work for numbers this big. Factoring big numbers is really, really hard. For now I would recommend that you fix your programs other problems, and accept that you're not going to be able to factor a number larger than 18446744073709551615 with it. – Steve Summit Jun 26 '22 at 12:07
  • As an example of what I'm talking about, try factoring the number 18446743979220271189. It fits in 64 bits just fine, but factoring it is going to take a while. (For an easier problem, that'll probably only take a few minutes, try 110167479599457199.) – Steve Summit Jun 26 '22 at 12:21
  • @SteveSummit thanks for your answer. My program find a first prime number and exit – iamMAHAM Jun 26 '22 at 14:58

2 Answers2

1

You are using a too big integer constant that can not be represented in any object of an integer type.

Consider this demonstration program.

#include <stdio.h>

#include <limits.h>
#include <inttypes.h>

int main( void )
{
    printf( "%llu\n", ULLONG_MAX );
    printf( "%" PRIuMAX "\n", UINTMAX_MAX );
}

Its output is

18446744073709551615
18446744073709551615

The outputted constant contains only 20 digits while your constant 1718944270642558716715u that you are trying to use contains 22 digits.

Pay attention to that in any case your function is incorrect. The function parameter has the type unsigned long long that you are assigning to a variable of the signed type long

void factor_prime(unsigned long long n)
{
    long i;
    long inter = n;
    //...

As you are trying to pass a very big number then the assignment results in getting an invalid value.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The problem with your code is that you're passing a value i.e. 1718944270642558716715u which is out of range for the unsigned long long numeric limit.

You can check the numeric limit of a type using:

  • For c++
std::numeric_limits<unsigned long long>::max() // 18446744073709551615
  • For C
#include <stdio.h>
#include <limits.h>

int main(void)
{   
    printf("%llu", ULONG_LONG_MAX); // 18446744073709551615
}   
Sidharth Mudgil
  • 1,293
  • 8
  • 25