平方锥体数(平方和)
平方锥体数表示自然数平方的和。自然数包括从 1 到无穷大的所有数字。例如,前 4 个平方锥体数是 1、5、14、30。
为了更好地理解,请考虑以下事实:如果我们取数量等于平方锥体数的球体,从 1 开始,并按降序堆叠它们,它们会形成一个金字塔。
问题陈述
给定一个数字 Sum。如果 Sum 是前“n”个自然数的平方和,则返回 n,否则返回 false。
示例 1
Input = 30 Output = 4
解释 = 30 是前 4 个自然数的平方和。
1*1 + 2*2 + 3*3 +4*4 = 30.
因此,输出应为 4。
示例 2
Input = 54 Output = -1
解释 = 没有 n 个自然数的平方和等于 54。因此,输出应为 -1。
问题陈述的解决方案
这个问题有 2 种解决方案。
方法 1:暴力法
暴力法是从 n= 1 开始。创建一个变量“total”并将下一个自然数的平方加到 total 的前一个值。如果 total 等于 Sum,则返回 n,否则如果 total 大于 Sum,则返回 false。
伪代码
start n =1 While (total < sum ): Total += n*n n=n+1 if(total == sum) : Return n Else: Return false end
示例
下面是一个 C++ 程序,用于检查给定数字是否为自然数平方的总和。
#include <iostream>
using namespace std;
// This function returns n if the sum of squares of first
// n natural numbers is equal to the given sum
// Else it returns -1
int square_pyramidal_number(int sum) {
// initialize total
int total = 0;
// Return -1 if Sum is <= 0
if(sum <= 0){
return -1;
}
// Adding squares of the numbers starting from 1
int n = 0;
while ( total < sum){
n++;
total += n * n;
}
// If total becomes equal to sum return n
if (total == sum)
return n;
return -1;
}
int main(){
int S = 30;
int n = square_pyramidal_number(S);
cout<< "Number of Square Pyramidal Numbers whose sum is 30: "<< endl;
(n) ? cout << n : cout << "-1";
return 0;
}
输出
Number of Square Pyramidal Numbers whose sum is 30: 4
时间复杂度 - O(sum),其中 sum 是给定的输入。
空间复杂度 - O(1):没有使用额外的空间。
方法 2:使用牛顿-拉夫森方法
另一种方法是牛顿-拉夫森方法。牛顿-拉夫森方法用于找到给定函数 f(x) 的根和根的初始猜测。
sum of squares of first n natural numbers = n * (n + 1) * (2*n + 1) / 6, n * (n + 1) * (2*n + 1) / 6 = sum or k * (k + 1) * (2*k + 1) – 6*sum = 0
所以 n 是这个三次方程的根,可以使用牛顿-拉夫森方法计算,该方法包括从初始猜测 x0 开始,牛顿-拉夫森方法使用以下公式来找到 x 的下一个值,即从前一个值 xn 计算 xn+1。
$$\mathrm{x_{1}=x_{0}-\frac{f(x_{0})}{f^{'}(x_{0})}}$$
伪代码
Start calculate func(x) and derivativeFunction(x) for given initial x Compute h: h = func(x) / derivFunc(x) While h is greater than allowed error ε h = func(x) / derivFunc(x) x = x – h If (x is an integer): return x Else: return -1; end
示例
下面是一个 C++ 程序,用于检查给定数字是否为自然数平方的总和。
#include<bits/stdc++.h>
#define EPSILON 0.001
using namespace std;
// According to Newton Raphson Method The function is
// k * (k + 1) * (2*k + 1) – 6*sum or 2*k*k*k + 3*k*k + k - 6*sum
double func(double k, int sum){
return 2*k*k*k + 3*k*k + k - 6*sum;
}
// Derivative of the above function is 6*k*k + 6*k + 1
double derivativeFunction(double k){
return 6*k*k + 6*k + 1;
}
// Function to check if double is an integer or not
bool isInteger(double N){
int X = N;
double temp2 = N - X;
if (temp2*10 >=1 ) {
return false;
}
return true;
}
// Function that finds the root of k * (k + 1) * (2*k + 1) – 6*sum
int newtonRaphson(double k, int sum){
double h = func(k, sum) / derivativeFunction(k);
while (abs(h) >= EPSILON){
h = func(k, sum)/derivativeFunction(k);
// k(i+1) = k(i) - f(k) / f'(k)
k = k - h;
}
if (isInteger(k)) {
return (int)k;
}
else {
return -1;
}
}
// Driver program
int main(){
double x0 = 1; // Initial values assumed
int sum = 91;
int n = newtonRaphson(x0,sum);
cout<< "Number of Square Pyramidal Numbers whose sum is 91: "<< endl;
(n) ? cout << n : cout << "-1";
return 0;
}
输出
Number of Square Pyramidal Numbers whose sum is 91: 6
时间复杂度 - O((log n) F(n)),其中 F(n) 是计算 f(x)/f'(x) 的成本,具有 n 位精度。
空间复杂度 - O(1):没有使用额外的空间。
结论
本文解决了为给定和查找平方锥体数的问题。我们看到了两种方法:暴力法和一种有效的方法。两种方法都提供了 C++ 程序。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP