Advertisement
myersjo

TriangluarStars4

Dec 6th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. /*A “star number”, s, is a number defined by the formula:  s = 6n(n-1) + 1  where n is the index
  2.  *of the star number. Thus the star numbers are:   1, 13, 37, 73, 121, 181, 253, …
  3.  *A “triangle number”, t, is the sum of the numbers from 1 to n:   t = 1 + 2 + … + (n-1) + n.  
  4.  *Thus the triangle numbers are:  1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
  5.  *153, 171, 190, 210, 231, 253, …
  6.  *Write a Java application that produces a list of all the values of type int that are simultaneously
  7.  *star numbers and triangle numbers.  As part of this problem you must write and use the following functions:
  8.  *      An isStarNumber() routine which determines if the passed number is a star number or not.
  9.  *      A determineStarNumber() function which returns the star number of a passed index (i.e. value of n).
  10.  */
  11. public class TriangularStars {
  12.  
  13.     public static int currentNumber = 0;
  14.     public static int previousNumber = 0;
  15.         public static void main(String[] args) {
  16.            
  17.                
  18.             System.out.println("The following are numbers which are both triangle numbers and star numbers: ");
  19.             int previousTriangleNumber=0;
  20.             int currentTriangleNumber=0;
  21.             int index=1;
  22.             while (currentNumber>=previousNumber)
  23.             {
  24.                 currentTriangleNumber = previousTriangleNumber + index;
  25.                 //System.out.println(currentTriangleNumber);
  26.                 previousNumber = currentNumber;
  27.                 if (isStarNumber(currentTriangleNumber))
  28.                 {
  29.                     System.out.print(currentTriangleNumber + ", ");
  30.                    
  31.                 }
  32.                 currentNumber = currentTriangleNumber;
  33.                 previousTriangleNumber = currentTriangleNumber;
  34.                 index++;
  35.             }
  36.  
  37.         }
  38.        
  39.         public static boolean isStarNumber (int number)
  40.         {
  41.            
  42.             boolean isStarNumber = false;
  43.             int index = 0;
  44.             int currentStarNumber = 0;
  45.             int previousStarNumber = 0;
  46.            
  47.             while (!isStarNumber && currentStarNumber <= number )
  48.             {
  49.                 previousStarNumber = currentStarNumber;
  50.                 currentStarNumber = determineStarNumber(index);
  51.                 //System.out.println(currentStarNumber + "; " + previousStarNumber);
  52.             /*    if (currentStarNumber < previousStarNumber)
  53.                 {
  54.                     isStarNumber = false;
  55.                     System.out.println(currentStarNumber);
  56.                     System.out.print("; " + index);
  57.                 }*/
  58.                 if (currentStarNumber == number)
  59.                 {
  60.                     isStarNumber = true;
  61.                 }
  62.                 else
  63.                     index += 1;
  64.                 }
  65.            
  66.             return (isStarNumber);
  67.         }
  68.        
  69.         public static int determineStarNumber (int index)
  70.         {
  71.             /*int starNumberValue = 0;
  72.             if (1 <= ((6*index)/Integer.MAX_VALUE) && 1 <= ((6*index) * (index-1)/Integer.MAX_VALUE))
  73.             {
  74.                 starNumberValue = (6*index) * (index-1) + 1;
  75.             }
  76.             else
  77.                 starNumberValue = -1;
  78.                 */
  79.             int starNumberValue = (6*index) * (index-1) + 1;
  80.             return (starNumberValue);
  81.            
  82.         }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement