0

I have a very long question and some part of the question, involves two functions named gp(a,t) and fibo(a,t).

gp(a,t) gets two integer numbers a ,t and must return the smallest prime number that is greater than or equal to (t/100)*a

fibo(a,t) gets two integer numbers a,t and must return the biggest member of fibonacci series that is less than or equal to (t/100)*a. (We consider 0 as the first member of fibonacci series in this problem, so it is 0 1 1 2 3 5 8 13 ...)

(The problem didn't mention that we must save that (t/100)*a as float but according to its examples, it must be considered a float for example if it is 3.5 then gp must return 5 and not 3.)

I implemented this functions but in some unknown test cases, it fails and so I want to know which part of my functions are wrong. Is there any a,t that my functions give wrong answer for them?

Note: I defined everything as long long because the main very long part of the question said to use long long because the numbers will get big.

Note 2: It doesn't give time limit error, so there is no problems like infinite loop or not optimised calculation of prime numbers.

My functions: (written in c)

long long int primeCheck(long long int a)
{
    if (a==1 || a ==0)
    {
        return 0;
    }
    long long int isPrime = 1;
    ;
    for (long long int i =2;i*i<=a;i++)
    {
        if (a%i==0)
        {
            isPrime=0;
            break;
        }
    }
    return isPrime;

}
long long int fibo (long long int a, long long int t)
{
    float check =  (t*1.00/100)*1.00*a;
    if (check ==0)
    {
        return 0;
    }
    else if (check==1)
    {
     return 1;
    }
    long long int f_n_2 = 0;
    long long int f_n_1 = 1;
    long long int f_n=0;

    while (f_n<=check)
    {
        f_n = f_n_2 + f_n_1;
        f_n_2 = f_n_1;
        f_n_1 = f_n;
        if (f_n > check)
        {
            return f_n_2;
        }
        else if (f_n == check)
        {
            return f_n;
        }

    }
    return 0;
}
long long int gp (long long int a , long long int t)
{

    float check =  (t*1.00/100)*1.00*a;
    long long int i=ceil(check);
    while(1)
    {
        if (primeCheck(i) )
        {
            return i;
        }
        i++;
    }
    return 0;
}

PS1. The problem is Solved. The issue was in the judge system and the code is just fine!

titansarus
  • 136
  • 1
  • 7
  • For `primeCheck`, special case 2 at start, then `for (long long int i = 3; i*i <= a; i += 2)` as there is no need to check every even number since checking 2 covers them all (i.e. check only odd at that point) – Craig Estey Oct 20 '18 at 19:13
  • @CraigEstey It is more optimised this way, but, the problem don't get time limit error so there is maybe a problem with the algorithm of the two functions. – titansarus Oct 20 '18 at 19:16
  • What values are allowed for `t` and `a`? – user3386109 Oct 20 '18 at 19:20
  • @ The question said they will be non negative integer numbers. (0 included). – titansarus Oct 20 '18 at 19:25
  • 1
    You're recalculating `i * i` on each iteration. Calculate the square root once and change the loop accordingly: `long long xsqrt = 0; for (; (xsqrt * xsqrt) < a; xsqrt += 1); for (long long i = 3; i <= xsqrt; i += 2)` – Craig Estey Oct 20 '18 at 19:25
  • I've done prime calculation with 7 different [progressively faster] algorithms with benchmarking here: https://pastebin.com/ce7wLuYT – Craig Estey Oct 20 '18 at 19:28
  • @CraigEstey It is now faster but nothing changed. It seems there maybe problem with gp and fibo functions. A very special test case for example that it fails on it. – titansarus Oct 20 '18 at 19:31
  • 2
    Note that `float check = (t*1.00/100)*1.00*a;` makes a mockery of using `long long`. The integer can handle up to 19 decimal digits; the `float` can't represent more than about 7 decimal digits. – Jonathan Leffler Oct 20 '18 at 19:32
  • @JonathanLeffler what is your suggestion then, using double? (I checked the double and nothing changed) – titansarus Oct 20 '18 at 19:33
  • 2
    Either `double` or (more likely) calculate with `long long` and not floating point. I don't see enough information on the ranges of `t` and `a` to know what's plausible input. – Jonathan Leffler Oct 20 '18 at 19:34
  • 1
    @JonathanLeffler Let's suppose that they are less than 1e14. the long long will have a problem when for example it gets 3.5 and in the gp part but it cuts to 3 and the answer will be 3 where the right answer is 5. – titansarus Oct 20 '18 at 19:37
  • If `t` and `a` are small enough, using `long long`, we could use scaled integer arithmetic which might be faster or more precise (e.g. `((t * 10000) / 100) * a` where `10000` is the scale factor) – Craig Estey Oct 20 '18 at 19:39
  • 1
    @titansarus The alternative to checking whether `f_n <= (t/100.0) * a` is to check whether `f_n * 100 <= t * a` The latter method eliminates the need for floating point math. So you don't need *any* `float` or `double` variables. – user3386109 Oct 20 '18 at 19:43
  • @user3386109 is there anythingI can do for gp by so I don't use ceil and double or float? – titansarus Oct 20 '18 at 19:48
  • 1
    @titansarus Yes, compute `(t*a)/100` and `(t*a) % 100`. If the latter is not zero, then add 1 to the former. That effectively calculates the ceiling without using double or float. – user3386109 Oct 20 '18 at 19:53
  • @user3386109 I changed all of them but it is still wrong. I don't really find any problem in the algorithm. – titansarus Oct 20 '18 at 20:11
  • @titansarus I would have to see the new code to know whether you got it all right. There's also the issue of the maximum values for `t` and `a`. – user3386109 Oct 20 '18 at 20:15
  • @user3386109: yeah, if `t` and `a` are big, it's likely that `t*a` overflows, invalidating the rest of the computation. – rici Oct 20 '18 at 20:53
  • @rici Yup, not only that, but OP's method of finding the fibonacci number could also overflow, since it computes the next fibonacci number greater than (t/100)*a. – user3386109 Oct 20 '18 at 21:07
  • OT: code should be written so it is very readable by humans. Function names like `gp()` is meaningless. variable names like: `f_n`, `f_n_1` etc etc are meaningless, even in the current context – user3629249 Oct 21 '18 at 03:13
  • suggest posting the actual 'online coding' question (or at least the link to it. so we can assure the posted code is actually trying to answer the question. BTW when asking a question about a run time problem, as this question is doing, post a [mcve] so we do not have to guess as to what your code actually is doing/ – user3629249 Oct 21 '18 at 03:17

0 Answers0