Stella Octangula Number In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Stella Octangula Number In C++

Stella Octangula Number In C++

BLUF: Mastering Stella Octangula Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Stella Octangula Number In C++

C++ is renowned for its efficiency. Learn how Stella Octangula Number In C++ enables low-level control and high-performance computing in the tutorial below.

Stella Octangula numbers represent a unique set of numbers with fascinating geometric and number theory characteristics. The term "Stella Octangula" originates from Latin, where "Stella" translates to "Star," and "Octangula" refers to an octahedron, an eight-faced polyhedron. These specific numbers are derived by applying a particular formula to generate the sequence. In the realm of mathematics, a Stella Octangula number belongs to the category of figurate numbers created from the Stella Octangula shape, expressed as n(2n2 - 1). Notably, the two Stella Octangula numbers that are perfect squares are 1 and 9653449.

Example:

Example

Input: 14
Output: Yes
For n=2, the value of n(n^2-1) is 14

Approach 1:

The simplest method to accomplish this task is by evaluating the expression n(2n2 - 1) starting from 0, and then comparing the result to x, which is the provided number. The process involves incrementing the value of n and recalculating n(2n2 - 1) until it matches x, signifying that x is a Stella octangula number. If the value of n surpasses x, then x is not a Stella octangula number.

Example:

Let's consider an example demonstrating the Stella Octangula Number in C++.

Example

#include <iostream>
using namespace std;
// The function that will check the number is Stella octangula 
int stellaOctangula(int key){
   // the initial value   
   int num = 0;
   // finding each value of n using 
   //the formula n(2*n*n-1) if the value
   //is less than x, then increment the value of n
   while(num*(2*num*num - 1)<key){
      num++;
   }
   // if the value is equal to x
   if(num*(2*num*num - 1)==key){
      return true;
   }
   // return
   return false;
}
int main(){
   int key = 14;
   if (stellaOctangula(key)){  
      cout << "Yes";
   }
   else{
      cout << "No";
   }
   return 0;
}

Output:

Explanation:

  • In this illustration, the StellaOctangula function is designed to accept an integer value as an argument and subsequently verify whether the number qualifies as a Stella Octangula number. This verification process involves iterating through the num values and confirming if the equation num(2num*num - 1) equals the provided key value. If this condition is met, the function returns true, indicating that the key indeed represents a Stella Octangula number.
  • Within the main function, a key variable is initialized with a value of 14. Subsequently, the stellaOctangula function is invoked with this key value. Following this invocation, the program outputs "Yes" if the function returns true and "No" if it returns false.
  • Approach 2:

It presents an alternative method to verify if a number qualifies as Stella Octangula. The process initiates at n=1, where each subsequent iteration updates the formula to acquire a fresh value: (2*n)^n^1. Upon substituting the exponent, the resulting expression becomes (2n)^ n^ -1. In this context, the discussion revolves around the X value and its associated expression value denoted as z = 0.

We aim to replace the variable with 'x' in the subsequent stage. Following this, we will determine the identity of each variable as 'x'. Furthermore, the verification process is employed to ascertain whether the condition holds true. The boolean type returns a true value if equality is met; otherwise, it proceeds to binary search. Our primary focus lies in validating whether 'x' and n^2-1 yield either a true or false outcome within the range of n/2 to n, emphasizing equality.

Example 2:

Let's consider another instance to demonstrate the concept of the Stella Octangula Number in C++.

Example

#include <bits/stdc++.h>
using namespace std;
// function for finding the 
//num*(2*num*num-1) value
int calculateExponent(int num) {
   return num*(2*num*num - 1);
}
// By using the binary search using the specific range
bool BinSearch(int l, int h, int key){
   while (l <= h) {
      int midval = (l + h) / 2;
      int val=calculateExponent(midval);
      if(val==key)
      {
          return true;
      }
      else if (val < key){
         l = midval + 1;
      }
      else {
         h= midval - 1;
      }
    
   }
   return false;
}
//each input value is checked
bool checkStellNumber(int val){
   // the base condition
   if (val == 0)
   return true;
   // double the value of num until 
   //it was lesser than val
   int num = 1;
   while (calculateExponent(num) < val){
      num= num*2;
   }
   //If the value is equal 
   //to val, then return true
   if (calculateExponent(num) == val){
      return true;
   }
   //The binary search method is called 
 return BinSearch(num/2, num, val);
}

int main(){
   int num = 14;
   if (checkStellNumber(num)){ 
      cout << "Yes";
   }
   else{
      cout << "No";
   }
   return 0;
}

Output:

Explanation:

  • calculateExponent(num): This function computes the expression num (2 num * num - 1), which is a necessary condition for the numbers belonging to Stella Octangula.
  • BinSearch(l, h, key): This function uses a binary search algorithm to find out the Stella Octangula numbers. It takes the limits of the lower and upper framing (L, H) and the key value (target) as input. If the calculated exponent is equal to the key, it is the targeted function. If true, it returns true, and if false, it returns false, signaling that the key is a Stella Octangula number. Otherwise, it changes the search area using comparisons between the computed exponent and the key.
  • checkStellNumber(val): It is the core functionality that invokes a test of whether the integer val is a Stella Octangula number. At first, it puts away the first base case, which is when val equals 0 (which is Stella Octangula). In the next stage, it finds the starting value of the binary search by squaring the value of num until the calculated root is greater than the value of val. At last, it will call the BinSarch function with the equal range of [num/2, num] and the target value val, and it will get back with the results.
  • Conclusion:

In summary, it will demonstrate the contrasts in an approach to effectively solving a challenging problem. The initial algorithm entails a combination of n/linear velocity with the addition of a linear expression -n^2 - n, resulting in (2n) for comparison with a traditional identity of x. Described by the author as "the slowest among the methods of the check," this initial approach involves solving all task problems, leading to a longer time required compared to the second algorithm. Consequently, the runtime is determined by the power of factor n multiplied by two and by n(2nˆn-1)/n. Subsequently, in the subsequent iteration, these two values are compared to x, reducing the search range by half. This process effectively narrows down the search range, explaining why the second algorithm outperforms the first; the doubling of the input size streamlines the solution process.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience