📜  斯特拉·奥克古拉(Stella Octangula)编号

📅  最后修改于: 2021-04-23 07:17:53             🧑  作者: Mango

给定数字n,请检查是否为Stella Octangula数字。数形式n(2n^{2} - 1)  其中n是整数(0,1,2,3,4,…)被称为Stella Octangula。 Stella Octangula的前几个数字是0、1、14、51、124、245、426、679、1016、1449、1990 …
斯特拉八角数是完美的平方是19653449
给定数字x,检查它是否为Stella octangula。
例子:

Input: x = 51
Output: Yes
For n = 3, the value of expression
n(2n2 - 1) is 51

Input: n = 53
Output: No

一个简单的解决方案是运行一个从n = 0开始的循环。对于每个n,检查n(2n 2 – 1)是否等于x。我们在n(2n 2 – 1)的值小于或等于x时运行循环。
一个有效的解决方案是使用无界二进制搜索。我们首先找到n的值,以便使用重复加倍使n(2n 2 – 1)大于x。然后我们应用二进制搜索。

C++
// Program to check if a number is Stella
// Octangula Number
#include 
using namespace std;
 
// Returns value of n*(2*n*n - 1)
int f(int n) {
   return n*(2*n*n - 1);
}
 
// Finds if a value of f(n) is equl to x
// where n is in interval [low..high]
bool binarySearch(int low, int high, int x)
{
    while (low <= high) {
        long long mid = (low + high) / 2;
 
        if (f(mid) < x)
            low = mid + 1;
        else if (f(mid) > x)
            high = mid - 1;
        else
            return true;
    }
    return false;
}
 
// Returns true if x isStella Octangula Number.
// Else returns false.
bool isStellaOctangula(int x)
{
    if (x == 0)
      return true;
 
    // Find 'high' for binary search by
    // repeated doubling
    int i = 1;
    while (f(i) < x)
        i = i*2;
 
    // If condition is satisfied for a
    // power of 2.
    if (f(i) == x)
        return true;
 
    //  Call binary search
    return binarySearch(i/2, i, x);
}
 
// driver code
int main()
{
    int n = 51;
    if (isStellaOctangula(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Program to check if
// a number is Stella
// Octangula Number
import java.io.*;
 
class GFG
{
 
// Returns value of
// n*(2*n*n - 1)
static int f(int n)
{
    return n * (2 * n *
                n - 1);
}
 
// Finds if a value of
// f(n) is equl to x
// where n is in
// interval [low..high]
static boolean binarySearch(int low,
                            int high,
                            int x)
{
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        if (f(mid) < x)
            low = mid + 1;
        else if (f(mid) > x)
            high = mid - 1;
        else
            return true;
    }
    return false;
}
 
// Returns true if x
// is Stella Octangula
// Number.Else returns
// false.
static boolean isStellaOctangula(int x)
{
    if (x == 0)
    return true;
 
    // Find 'high' for
    // binary search by
    // repeated doubling
    int i = 1;
    while (f(i) < x)
        i = i * 2;
 
    // If condition is
    // satisfied for a
    // power of 2.
    if (f(i) == x)
        return true;
 
    // Call binary search
    return binarySearch(i / 2,
                        i, x);
}
 
// Driver code
public static void main (String[] args)
{
    int n = 51;
    if (isStellaOctangula(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python3 program to check if a number
# is Stella octangula number
 
# Returns value of n*(2*n*n - 1)
def f(n):
     
    return n * (2 * n * n - 1);
 
# Finds if a value of f(n) is equl to x
# where n is in interval [low..high]
def binarySearch(low, high, x):
 
    while (low <= high):
        mid = int((low + high) // 2);
 
        if (f(mid) < x):
            low = mid + 1;
        elif (f(mid) > x):
            high = mid - 1;
        else:
            return True;
             
    return False;
 
# Returns true if x isStella octangula
#  number. Else returns false.
def isStellaOctangula(x):
 
    if (x == 0):
        return True;
 
    # Find 'high' for binary search
    # by repeated doubling
    i = 1;
    while (f(i) < x):
        i = i * 2;
 
    # If condition is satisfied for a
    # power of 2.
    if (f(i) == x):
        return True;
 
    # Call binary search
    return binarySearch(i / 2, i, x);
 
# Driver code
n = 51;
 
if (isStellaOctangula(n) == True):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech


C#
// Program to check if
// a number is Stella
// Octangula Number
using System;
 
class GFG
{
 
// Returns value of
// n*(2*n*n - 1)
static int f(int n)
{
    return n * (2 * n *
                n - 1);
}
 
// Finds if a value of
// f(n) is equl to x
// where n is in
// interval [low..high]
static bool binarySearch(int low,
                         int high,
                         int x)
{
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        if (f(mid) < x)
            low = mid + 1;
        else if (f(mid) > x)
            high = mid - 1;
        else
            return true;
    }
    return false;
}
 
// Returns true if x
// is Stella Octangula
// Number.Else returns
// false.
static bool isStellaOctangula(int x)
{
    if (x == 0)
    return true;
 
    // Find 'high' for
    // binary search by
    // repeated doubling
    int i = 1;
    while (f(i) < x)
        i = i * 2;
 
    // If condition is
    // satisfied for a
    // power of 2.
    if (f(i) == x)
        return true;
 
    // Call binary search
    return binarySearch(i / 2,
                        i, x);
}
 
// Driver code
public static void Main ()
{
    int n = 51;
    if (isStellaOctangula(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed
// by anuj_67.


Javascript


输出:
Yes