Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:12:27+00:00 2026-06-15T09:12:27+00:00

This is the question I’ve been assigned: A so-called star number, s, is a

  • 0

This is the question I’ve been assigned:

A so-called “star number”, s, is a number defined by the formula:
s = 6n(n-1) + 1
where n is the index of the star number.
Thus the first six (i.e. for n = 1, 2, 3, 4, 5 and 6) star numbers are: 1, 13, 37,
73, 121, 181

In contrast a so-called “triangle number”, t, is the sum of the numbers from 1 to n: t = 1 + 2 + … + (n-1) + n.
Thus the first six (i.e. for n = 1, 2, 3, 4, 5 and 6) triangle numbers are: 1, 3, 6, 10, 15, 21

Write a Java application that produces a list of all the values of type int that are both star number and triangle numbers.

When solving this problem you MUST write and use at least one function (such as isTriangeNumber() or isStarNumber()
or determineTriangeNumber() or determineStarNumber()). Also you MUST only use the formulas provided here to solve the problem.

tl;dr: Need to output values that are both Star Numbers and Triangle Numbers.

Unfortunately, I can only get the result to output the value ‘1’ in an endless loop, even though I am incrementing by 1 in the while loop.

public class TriangularStars {
    public static void main(String[] args) {

    int n=1;            
    int starNumber = starNumber(n);
    int triangleNumber = triangleNumber(n);

    while ((starNumber<Integer.MAX_VALUE)&&(n<=Integer.MAX_VALUE))
    {
        if ((starNumber==triangleNumber)&& (starNumber<Integer.MAX_VALUE))
                {
                    System.out.println(starNumber);
                }
        n++;
    }
  }


public static int starNumber( int n)
{
    int starNumber;
    starNumber= (((6*n)*(n-1))+1);
    return starNumber;

}
public static int triangleNumber( int n)
{
    int triangleNumber;
    triangleNumber =+ n;
    return triangleNumber;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:12:28+00:00Added an answer on June 15, 2026 at 9:12 am

    Here’s a skeleton. Finish the rest yourself:

    Questions to ask yourself:

    1. How do I make a Triangle number?
    2. How do I know if something is a Star number?
    3. Why do I only need to proceed until triangle is negative? How can triangle ever be negative?

    Good luck!

    public class TriangularStars {
      private static final double ERROR = 1e-7;
    
      public static void main(String args[]) {
        int triangle = 0;
        for (int i = 0; triangle >= 0; i++) {
          triangle = determineTriangleNumber(i, triangle);
          if (isStarNumber(triangle)) {
            System.out.println(triangle);
          }
        }
      }
    
      public static boolean isStarNumber(int possibleStar) {
        double test = (possibleStar - 1) / 6.;
        int reduce = (int) (test + ERROR);
        if (Math.abs(test - reduce) > ERROR)
          return false;
    
        int sqrt = (int) (Math.sqrt(reduce) + ERROR);
        return reduce == sqrt * (sqrt + 1);
      }
    
      public static int determineTriangleNumber(int i, int previous) {
        return previous + i;
      }
    }
    

    Output:

    1
    253
    49141
    9533161
    1849384153
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked many times before, but they all seem to relate
This question has been asked before but the accepted solution (given by the question
This question has been asked a lot of times in many forums. But I
This question has been thrown around without an answer. YES, we can hide the
This question is totally unacceptable and will be closed but... Has SQL Fiddle been
This question has been retitled/retagged so that others may more easily find the solution
This question might have been answered, if yes, please share the link. I have
This question has been asked multiple times on SO but all the answers refer
This question has been bothering me forever. I can wait to hear the responses.
This question is directly related to this SO question I posed about 15 minutes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.