After Aseem Bansal Aseem Bansal's answer I try to find a more optimize way to solve the problem and i came up with this code
After Aseem Bansal's answer I try to find a more optimize way to solve the problem and i came up with this code
After Aseem Bansal's answer I try to find a more optimize way to solve the problem and i came up with this code
After Aseem Bansal's answer I try to find a more optimizingoptimize way to solve the problem and i came up with this code
After Aseem Bansal's answer I try to find a more optimizing way to solve the problem and i came up with this code
After Aseem Bansal's answer I try to find a more optimize way to solve the problem and i came up with this code
- 2k
- 15
- 33
UPDATE 1
After Aseem Bansal 's answer I try to find a more optimizing way to solve the problem and i came up with this code
#include<stdio.h>
int main(void)
{
int input, result = 0;
printf("Enter the input = ");
scanf("%d",&input);
while(input <= 0) // if input is negative ask again
{
printf("Enter a positive non zero number :");
scanf("%d",&input);
}
int middle = (input/2);
if(input%2 != 0) // if the input is an odd number
{
result = (middle * (middle+1));
}
else // if input is an even number
{
result = middle%2 == 0 ? ((middle-1) * (middle+1))
: ((middle-2) * (middle+2));
}
printf("result = %d",result);
return 0;
}
OUTPUT 1
Enter the input = 13
result = 42
OUTPUT 2
Enter the input = 12
result = 35
OUTPUT 3
Enter the input = 10
result = 21
EXPLANATION
As for any odd number the middle
and middle+1
are highest coprime numbers, so the GCD will be 1. Therefore LCM
will be product of middle
and middle+1
.
But for any even number the LCM
of middle
and middle+1
is 1. So we need to find the nearest coprime numbers. If middle
is even then the nearest coprime numbers are middle-1
and middle+1
otherwise the nearest coprime numbers are middle-2
and middle+2
. The product will be the result.
NOTE
here middle
is floor of (middle
).
Can it be more optimized? Any review is welcome.
Can it be more optimized?
UPDATE 1
After Aseem Bansal 's answer I try to find a more optimizing way to solve the problem and i came up with this code
#include<stdio.h>
int main(void)
{
int input, result = 0;
printf("Enter the input = ");
scanf("%d",&input);
while(input <= 0) // if input is negative ask again
{
printf("Enter a positive non zero number :");
scanf("%d",&input);
}
int middle = (input/2);
if(input%2 != 0) // if the input is an odd number
{
result = (middle * (middle+1));
}
else // if input is an even number
{
result = middle%2 == 0 ? ((middle-1) * (middle+1))
: ((middle-2) * (middle+2));
}
printf("result = %d",result);
return 0;
}
OUTPUT 1
Enter the input = 13
result = 42
OUTPUT 2
Enter the input = 12
result = 35
OUTPUT 3
Enter the input = 10
result = 21
EXPLANATION
As for any odd number the middle
and middle+1
are highest coprime numbers, so the GCD will be 1. Therefore LCM
will be product of middle
and middle+1
.
But for any even number the LCM
of middle
and middle+1
is 1. So we need to find the nearest coprime numbers. If middle
is even then the nearest coprime numbers are middle-1
and middle+1
otherwise the nearest coprime numbers are middle-2
and middle+2
. The product will be the result.
NOTE
here middle
is floor of (middle
).
Can it be more optimized? Any review is welcome.
- 2.3k
- 3
- 22
- 37