Program to evaluate the expression (√X+1)^6 + (√X-1)^6
Last Updated :
19 Jul, 2022
Given a number . The task is to find the value of the below expression for the given value of .
Examples:
Input: X = ?2
Output: 198
Explanation:
= 198
Input: X = 3
Output: 4160
Approach: The idea is to use Binomial expression. We can take these two terms as 2 binomial expressions. By expanding these terms we can find the desired sum. Below is the expansion of the terms.
Now put X= in EQ(1)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float calculateSum( float n)
{
int a = int (n);
return 2 * ( pow (n, 6) + 15 * pow (n, 4)
+ 15 * pow (n, 2) + 1);
}
int main()
{
float n = 1.4142;
cout << ceil (calculateSum(n)) << endl;
return 0;
}
|
Java
import java.util.*;
class gfg
{
public static double calculateSum( double n)
{
return 2 * (Math.pow(n, 6 ) + 15 * Math.pow(n, 4 )
+ 15 * Math.pow(n, 2 ) + 1 );
}
public static void main(String[] args)
{
double n = 1.4142 ;
System.out.println(( int )Math.ceil(calculateSum(n)));
}
}
|
Python3
import math
def calculateSum(n):
a = int (n)
return ( 2 * ( pow (n, 6 ) + 15 * pow (n, 4 )
+ 15 * pow (n, 2 ) + 1 ))
if __name__ = = '__main__' :
n = 1.4142
print (math.ceil(calculateSum(n)))
|
C#
using System;
class gfg
{
public static double calculateSum( double n)
{
return 2 * (Math.Pow(n, 6) + 15 * Math.Pow(n, 4)
+ 15 * Math.Pow(n, 2) + 1);
}
public static int Main()
{
double n = 1.4142;
Console.WriteLine(Math.Ceiling(calculateSum(n)));
return 0;
}
}
|
PHP
<?php
function calculateSum( $n )
{
$a = (int) $n ;
return (2 * (pow( $n , 6) +
15 * pow( $n , 4) +
15 * pow( $n , 2) + 1));
}
$n = 1.4142;
echo ceil (calculateSum( $n ));
?>
|
Javascript
<script>
function calculateSum(n)
{
return 2 * (Math.pow(n, 6) + 15 * Math.pow(n, 4)
+ 15 * Math.pow(n, 2) + 1);
}
var n = 1.4142;
document.write(parseInt(Math.ceil(calculateSum(n))));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...