-2

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?

Input Format : First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.

Output Format : Print the required answer for each test case.

Constraints : 1≤T≤10 1≤N≤40

full link to the question

Here is code whose results were accepted by hackerrank but i am having trouble understanding the solution.

Can anyone please explain this?

what does the line ans *= i / (ans % i) do? Rest of it i understood.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

bool check_if_prime(long n);

int main(void) {
    long t, n, i, ans = 1;
    std::cin >> t;
    while(t--){
        std::cin >> n;
        for(i = 2; i <= n; ++i){
            if(!check_if_prime(i)){
                if(ans % i)
                    ans *= i / (ans % i);
            }else
                ans *= i;
        }
        std::cout << ans << std::endl;
        ans = 1;
    }
    return 0;
}

bool check_if_prime(long n){
    if(n == 2)
        return true;
    for(long i = 2; i * i <= n; ++i){
        if(n % i == 0)
            return false;
    }
    return true;
}
glear14195
  • 80
  • 1
  • 9
Aneesh Dandime
  • 123
  • 1
  • 11
  • 1
    Your debugger can. Did you ask it? – Bathsheba Jun 12 '15 at 12:51
  • 3
    It seems pretty basic and straightforward. *What* are you not understanding? – nvoigt Jun 12 '15 at 12:52
  • ans % i is the GCD of 'ans' and 'i' ? @nvoigt – Aneesh Dandime Jun 12 '15 at 12:57
  • @AneeshDandime No, it's not. The `%` operator is well-documented. Which language reference, if any, are you using that you're having trouble finding it? –  Jun 12 '15 at 13:30
  • @hvd what does **ans *= i / (ans % i)** do? logically it should give the lcm of 'ans' and 'i' right? – Aneesh Dandime Jun 12 '15 at 13:39
  • @AneeshDandime No. Again, `ans % i` isn't the GCD. Look up what it does to in your language reference. Since `ans % i` isn't the GCD, `ans *= i / (ans % i);` can't calculate the LCM. –  Jun 12 '15 at 13:45
  • @hvd i know that % gives the remainder. :( – Aneesh Dandime Jun 12 '15 at 13:54
  • @AneeshDandime Then sorry, I don't understand how you think `ans *= i / (ans % i)` calculates the LCM. `ans *= i / gcd(ans, i)` would calculate the LCM, and (as you now know) `ans % i` isn't `gcd(ans, i)`, so `ans *= i / (ans % i)` necessarily calculates something else. I don't think there's a common name for that operation. It's simply three operations combined into one, and presumably you already know `*` and `/`. –  Jun 12 '15 at 14:02
  • @hvd His problem (and honestly mine too) is mathematical, not related to C++. He knows what `*`, `/` and `%` do. The point is, WHY does it work to calculate that? If `ans` can't be divided by `i`, the easiest way to make it divisible by `i` is to multiply it by `i`. But this could be too much: if you have `ans=6` and `i=4`, you don't want `ans *= 4`, it would be too high, you actually want `ans *= 2`. Here it is achieved by multiplying by `i` and dividing by `ans % i`. The point is: why does it work, **mathematically**, to divide by `ans % i`? – Fabio says Reinstate Monica Jun 12 '15 at 14:45
  • @hvd So, **i / (ans % i)** basically gives the number to be multiplied to **ans** so that **ans** is now a divisible by **i** . – Aneesh Dandime Jun 12 '15 at 14:46
  • @FabioTurati Mostly agreed. The OP's problem is both mathematical and related to C++. In the earlier comments, the OP suggested that the `%` operator returns the GCD. That part is not a mathematical problem. The remainder (no pun intended) is. –  Jun 12 '15 at 14:47
  • @FabioTurati oh finally. thank u so much. My problem is exactly what u described in your comment! Sorry for all this confusion. I did not explain my doubt properly. – Aneesh Dandime Jun 12 '15 at 14:54
  • @hvd so now can u please explain how the line works mathematically? – Aneesh Dandime Jun 12 '15 at 14:58
  • My hunch is that, somehow, it must be true that `ans % i` does in fact equal `gcd(ans, i)`, due to the specific way `ans` is constructed (`ans` is a multiple of every number up to but not including `i`). In other words, Euclidean algorithm applied to `ans` and `i` would always complete in a single iteration. Formal proof of this proposition eludes me at the moment, however. – Igor Tandetnik Jun 12 '15 at 22:31
  • @IgorTandetnik Same here, i am unable to prove it. – Aneesh Dandime Jun 13 '15 at 02:09
  • @Igor: same here, I think it works like the gcd but I can't understand why. Aneesh: maybe you could ask this question on http://math.stackexchange.com/, I think there's a very good chance someone there can answer. If you do, please post the link here, I'd be curious to read it. – Fabio says Reinstate Monica Jun 13 '15 at 02:33
  • @FabioTurati okay i will ask this question on [link](math.stackexchange.com). If anyone turns up with the answer i will post the link here. – Aneesh Dandime Jun 13 '15 at 03:31
  • @FabioTurati see the accepted answer below. It proves that the algorithm fails. – Aneesh Dandime Jun 16 '15 at 14:00
  • @AneeshDandime Thank you for taking the time to notify me, I really appreciate it. So basically the algorithm doesn't really work, it just happens to work, by chance, for small values. So much for the "pretty basic and straightforward" logic, and the debugger that "can understand it" as others said. I'm just sorry my upvote alone can't compensate the downvotes you got. Somebody even downvoted glear14195's other question. Sometimes I really hate this site... – Fabio says Reinstate Monica Jun 16 '15 at 14:33
  • @FabioTurati its okay. Nevermind the downvotes. :) – Aneesh Dandime Jun 16 '15 at 14:35

1 Answers1

3

The above code does not give the right output for a number of test cases. For example:

    Output     N   Correct Answer
    232792560  19  232792560
    1059261584 23  5354228880
    1117182544 25  26771144400
    1886839328 27  80313433200

You might want to check out Pham Trung's answer on a similar question I asked.

Community
  • 1
  • 1
glear14195
  • 80
  • 1
  • 9