Skip to main content

Find star number

The concept of star numbers is a mathematical sequence defined by a formula. These numbers are called star numbers because when they are represented visually, they form star shapes. In this article, we will discuss the star number sequence, provide an explanation of the problem statement, present an algorithm and pseudocode to find star numbers, and finally, explain the output of the given code snippet.

Problem Statement

The task is to find the star numbers up to a given value, k. Star numbers are generated using the formula: 6n(n - 1) + 1, where n represents the index of the star number. The program should print the star numbers from index 1 up to k.

For example, if k is 10, the expected output would be: 1 13 37 73 121 181 253 337 433 541.

Algorithm and Pseudocode

The algorithm to find star numbers up to index k is straightforward. We will use a loop to iterate through the indices from 1 to k, and for each index, we will calculate the corresponding star number using the formula mentioned earlier. The pseudocode for this algorithm is as follows:


starNo(k)
    for n = 1 to k
        result = 6 * n * (n - 1) + 1
        print result

The starNo function takes the value of k as input and iterates through the indices using the variable n. For each iteration, it calculates the star number using the formula and prints the result.

Code Solution

Here given code implementation process.

// C Program for
// Find star number
#include <stdio.h>

void starNo(int k)
{
	// Print all initial k star number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//   6n(n − 1) + 1
      
		// Calculate nth star number
		int result = (6 *n *(n - 1) + 1);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
    // 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	starNo(10);
	return 0;
}

Output

  1  13  37  73  121  181  253  337  433  541
// Java program for
// Find star number
public class StarNumber
{
	public void starNo(int k)
	{
		// Print all initial k star number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			int result = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		StarNumber task = new StarNumber();
		// Star number are
		// —————————————————————————————————————————————
		// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
		// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
		// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
		// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
		// k = 10
		task.starNo(10);
	}
}

Output

 1 13 37 73 121 181 253 337 433 541
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find star number

class StarNumber
{
	public: void starNo(int k)
	{
		// Print all initial k star number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			int result = (6 *n *(n - 1) + 1);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	StarNumber *task = new StarNumber();
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	task->starNo(10);
	return 0;
}

Output

 1 13 37 73 121 181 253 337 433 541
// Include namespace system
using System;
// Csharp program for
// Find star number
public class StarNumber
{
	public void starNo(int k)
	{
		// Print all initial k star number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			int result = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		StarNumber task = new StarNumber();
		// Star number are
		// —————————————————————————————————————————————
		// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
		// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
		// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
		// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
      
		// k = 10
		task.starNo(10);
	}
}

Output

 1 13 37 73 121 181 253 337 433 541
package main
import "fmt"
// Go program for
// Find star number

func starNo(k int) {
	// Print all initial k star number
	for n := 1 ; n <= k ; n++ {
		// Formula
		// 6n(n − 1) + 1
		// Calculate nth star number
		var result int = (6 * n * (n - 1) + 1)
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	starNo(10)
}

Output

 1 13 37 73 121 181 253 337 433 541
<?php
// Php program for
// Find star number
class StarNumber
{
	public	function starNo($k)
	{
		// Print all initial k star number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			$result = (6 * $n * ($n - 1) + 1);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new StarNumber();
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	$task->starNo(10);
}
main();

Output

 1 13 37 73 121 181 253 337 433 541
// Node JS program for
// Find star number
class StarNumber
{
	starNo(k)
	{
		// Print all initial k star number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			var result = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new StarNumber();
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	task.starNo(10);
}
main();

Output

 1 13 37 73 121 181 253 337 433 541
#  Python 3 program for
#  Find star number
class StarNumber :
	def starNo(self, k) :
		n = 1
		#  Print all initial k star number
		while (n <= k) :
			#  Formula
			#  6n(n − 1) + 1
            
			#  Calculate nth star number
			result = (6 * n * (n - 1) + 1)

			#  Display calculated result
			print(" ", result, end = "")
			n += 1
		
	

def main() :
	task = StarNumber()
	#  Star number are
	#  —————————————————————————————————————————————
	#  1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	#  793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	#  2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	#  4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
    
	#  k = 10
	task.starNo(10)

if __name__ == "__main__": main()

Output

  1  13  37  73  121  181  253  337  433  541
#  Ruby program for
#  Find star number
class StarNumber 
	def starNo(k) 
		n = 1
		#  Print all initial k star number
		while (n <= k) 
			#  Formula
			#  6n(n − 1) + 1
            
			#  Calculate nth star number
			result = (6 * n * (n - 1) + 1)

			#  Display calculated result
			print(" ", result)
			n += 1
		end

	end

end

def main() 
	task = StarNumber.new()
	#  Star number are
	#  —————————————————————————————————————————————
	#  1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	#  793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	#  2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	#  4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
    
	#  k = 10
	task.starNo(10)
end

main()

Output

 1 13 37 73 121 181 253 337 433 541
// Scala program for
// Find star number
class StarNumber()
{
	def starNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k star number
		while (n <= k)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			var result: Int = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: StarNumber = new StarNumber();
		// Star number are
		// —————————————————————————————————————————————
		// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
		// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
		// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
		// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
		// k = 10
		task.starNo(10);
	}
}

Output

 1 13 37 73 121 181 253 337 433 541
// Swift 4 program for
// Find star number
class StarNumber
{
	func starNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k star number
		while (n <= k)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			let result: Int = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: StarNumber = StarNumber();
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
  
	// k = 10
	task.starNo(10);
}
main();

Output

  1  13  37  73  121  181  253  337  433  541
// Kotlin program for
// Find star number
class StarNumber
{
	fun starNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k star number
		while (n <= k)
		{
			// Formula
			// 6n(n − 1) + 1
          
			// Calculate nth star number
			val result: Int = (6 * n * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: StarNumber = StarNumber();
	// Star number are
	// —————————————————————————————————————————————
	// 1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661, 
	// 793, 937, 1093, 1261, 1441, 1633, 1837, 2053, 2281, 
	// 2521, 2773, 3037, 3313, 3601, 3901, 4213, 4537, 
	// 4873, 5221, 5581, 5953, 6337, 6733, 7141 ... etc
	// k = 10
	task.starNo(10);
}

Output

 1 13 37 73 121 181 253 337 433 541

Explanation and Output

Now, let's analyze the given code and understand its output. The code snippet is written in the C programming language and uses the printf function to print the star numbers. The main function calls the starNo function with the value 10, indicating that it wants to find the star numbers up to the 10th index.

The output of the code is:
1 13 37 73 121 181 253 337 433 541

The code correctly generates and prints the star numbers from the 1st index up to the 10th index. These numbers are obtained by applying the star number formula to each index and displaying the result.

Time Complexity

The time complexity of the given code is O(k), where k represents the input value passed to the starNo function. The loop iterates k times to calculate and print the star numbers. Therefore, the time taken by the code depends linearly on the input value k.

Finally

In this article, we explored the concept of star numbers, which are generated using a specific formula. We discussed the problem statement and provided an algorithm to find star numbers up to a given index. The pseudocode was presented to illustrate the steps of the algorithm. Additionally, we analyzed the output of the provided code snippet and explained how it correctly prints the star numbers. Finally, we discussed the time complexity of the code, which is linear with respect to the input value. By understanding this concept and implementing the algorithm, you can find star numbers for any desired index.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment