samedi 4 juin 2016

Why this solution is not accepted ? Am I missing something ?

**Here's a question from a competitive coding platform.I Think I have attempted it correctly. I solved it manually and cross checked the result. But its not getting accepted. Even a small help will be appreciated **

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 lines, each containing an integer,N .

Output Format

Print the required answer for each test case.

Constraints

1 <= T <= 10

1<= N<= 40

#include<bits/stdc++.h>
using namespace std;
long long int calculate_HCF( long long int a , long long int b)
{
if( b == 0)
        return a;
else
        return calculate_HCF(b , a%b);
}
long long int calculate_LCM( long long int a , long long int b)
{

  long  long int HCF = calculate_HCF(a , b);
  long  long  int LCM = (a*b)/HCF;
    return LCM;
}
int main()
{
int T;
int n;
long long int LCM=1;
scanf("%d" , &T);
    {
    for ( int i = 0 ; i < T ; i++)
        {
        scanf("%d" , &n);
            if( n == 1)
                {
                printf("%d\n",1);

                    continue;
            }
            for( long long int p = 2 ; p <= n ; p++)
                {
                    LCM = calculate_LCM(LCM , p);
            }
            printf("%lld\n",LCM);
    }
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire