Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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

grammar issue
Source Link

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

give explanation and add new code
Source Link

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.

Tweeted twitter.com/#!/StackCodeReview/status/353561439128129536
added 22 characters in body
Source Link
Loading
Better title, might give more attention to the question
Link
Loading
Loading
added 740 characters in body
Source Link
Loading
Source Link
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /