Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java finding numbers that are both a Triangle Number and a Star Number

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;
}

}

like image 929
studentcoder Avatar asked Jan 26 '26 03:01

studentcoder


1 Answers

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
like image 196
durron597 Avatar answered Jan 28 '26 15:01

durron597



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!