编写程序来查找前n个奇数自然数的四次方和。
1 4 + 3 4 + 5 4 + 7 4 + 9 4 + 11 4 …………。+(2n-1) 4 。
例子:
Input : 3
Output : 707
14 +34 +54 = 707
Input : 6
Output : 24310
14 + 34 + 54 + 74 + 94 + 114
天真的方法:-在这个简单的查找中,前n个奇数自然数的第四次幂是从1到n次循环循环,并将结果存储在变量和中。
例-n = 3那么(1 * 1 * 1 * 1)+(3 * 3 * 3 * 3)+(5 * 5 * 5 * 5)= 707
C++
// CPP Program to find the sum of fourth powers
// of first n odd natural numbers
#include
using namespace std;
// calculate the sum of fourth power of first
// n odd natural numbers
long long int oddNumSum(int n)
{
int j = 0;
long long int sum = 0;
for (int i = 1; i <= n; i++) {
j = (2 * i - 1);
sum = sum + (j * j * j * j);
}
return sum;
}
// Driven Program
int main()
{
int n = 6;
cout << oddNumSum(n) << endl;
return 0;
}
Java
// Java Program to find the
// sum of fourth powers of
// first n odd natural numbers
import java.io.*;
class GFG {
// calculate the sum of
// fourth power of first
// n odd natural numbers
static long oddNumSum(int n)
{
int j = 0;
long sum = 0;
for (int i = 1; i <= n; i++) {
j = (2 * i - 1);
sum = sum + (j * j * j * j);
}
return sum;
}
// Driven Program
public static void main(String args[])
{
int n = 6;
System.out.println(oddNumSum(n));
}
}
// This code is contributed
// by Nikita tiwari.
Python 3
# Python 3 Program to find the
# sum of fourth powers of
# first n odd natural numbers
# calculate the sum of
# fourth power of first
# n odd natural numbers
def oddNumSum(n) :
j = 0
sm = 0
for i in range(1, n + 1) :
j = (2 * i - 1)
sm = sm + (j * j * j * j)
return sm
# Driven Program
n = 6;
print(oddNumSum(n))
# This code is contributed
# by Nikita tiwari.
C#
// C# Program to find the
// sum of fourth powers of
// first n odd natural numbers
using System;
class GFG {
// calculate the sum of
// fourth power of first
// n odd natural numbers
static long oddNumSum(int n)
{
int j = 0;
long sum = 0;
for (int i = 1; i <= n; i++) {
j = (2 * i - 1);
sum = sum + (j * j * j * j);
}
return sum;
}
// Driven Program
public static void Main()
{
int n = 6;
Console.Write(oddNumSum(n));
}
}
// This code is contributed by
// vt_m.
PHP
Javascript
C++
// CPP Program to find the sum of fourth powers
// of first n odd natural numbers
#include
using namespace std;
// calculate the sum of fourth power of first
// n odd natural numbers
long long int oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
int main()
{
int n = 4;
cout << oddNumSum(n) << endl;
return 0;
}
Java
// Java Program to find the sum of
// fourth powers of first n odd
// natural numbers
class GFG {
// calculate the sum of fourth
// power of first n odd natural
// numbers
static long oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
public static void main(String[] args)
{
int n = 4;
System.out.println(oddNumSum(n));
}
}
// This code is contributed by
// Smitha Dinesh Semwal.
Python 3
# Python 3 Program to find the
# sum of fourth powers of first
# n odd natural numbers
# calculate the sum of fourth
# power of first n odd natural
#numbers
def oddNumSum(n):
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15
# Driven Program
n = 4
print(int(oddNumSum(n)))
# This code is contributed by
# Smitha Dinesh Semwal.
C#
// C# Program to find the sum of
// fourth powers of first n
// odd natural numbers
using System;
class GFG {
// calculate the sum of fourth
// power of first n odd
// natural numbers
static long oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
public static void Main()
{
int n = 4;
Console.Write(oddNumSum(n));
}
}
// This code is contributed by
// vt_m.
PHP
Javascript
输出:
24310
时间复杂度:O(N)
有效的方法:-一个有效的解决方案是使用直接的数学公式,它是:
Fourth power natural number = (14 + 24 + 34 + ………… +n4)
= (n(n+1)(2n+1)(3n2+3n-1))/30
Fourth power even natural number = (24 + 44 + 64 + ………… +2n4)
= 8(n(n+1)(2n+1)(3n2+3n-1))/15;
We need odd natural number so we subtract the
(Fourth power odd natural number) = (Fourth power first n natural number) – (Fourth power even natural number)
= (14 + 24 + 34 + ………… +n4) – (24 + 44 + 64 + ………… +2n4)
= (14 + 34 + 54 + ………… +(2n-1)4)
drives formula
= (2n(2n+1)(4n+1)(12n2+6n-1))/30 – (8(n(n+1)(2n+1)(3n2+3n -1)))/15
= 2n(2n+1)/30[(4n+1)(12n2+6n-1) – ((8n+8)((3n2+3n-1))]
= n(2n+1)/15[(48n3 + 24n 2 – 4n + 12n2 + 6n -1) – (24n3 + 24n 2 – 8n + 24n2 + 24n -8) ]
= n(2n+1)/15[24n3 – 12n2 – 14n + 7]
C++
Sum of fourth power of first n odd numbers = n(2n+1)/15[24n3 - 12n2 - 14n + 7]
Java
// CPP Program to find the sum of fourth powers
// of first n odd natural numbers
#include
using namespace std;
// calculate the sum of fourth power of first
// n odd natural numbers
long long int oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
int main()
{
int n = 4;
cout << oddNumSum(n) << endl;
return 0;
}
的Python 3
// Java Program to find the sum of
// fourth powers of first n odd
// natural numbers
class GFG {
// calculate the sum of fourth
// power of first n odd natural
// numbers
static long oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
public static void main(String[] args)
{
int n = 4;
System.out.println(oddNumSum(n));
}
}
// This code is contributed by
// Smitha Dinesh Semwal.
C#
# Python 3 Program to find the
# sum of fourth powers of first
# n odd natural numbers
# calculate the sum of fourth
# power of first n odd natural
#numbers
def oddNumSum(n):
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15
# Driven Program
n = 4
print(int(oddNumSum(n)))
# This code is contributed by
# Smitha Dinesh Semwal.
的PHP
// C# Program to find the sum of
// fourth powers of first n
// odd natural numbers
using System;
class GFG {
// calculate the sum of fourth
// power of first n odd
// natural numbers
static long oddNumSum(int n)
{
return (n * (2 * n + 1) *
(24 * n * n * n - 12 * n
* n - 14 * n + 7)) / 15;
}
// Driven Program
public static void Main()
{
int n = 4;
Console.Write(oddNumSum(n));
}
}
// This code is contributed by
// vt_m.
Java脚本
输出:
时间复杂度:O(1)