-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangularStars.java
More file actions
59 lines (57 loc) · 1.48 KB
/
triangularStars.java
File metadata and controls
59 lines (57 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class triangularStars {
public static final int BEGINNING_VALUE=1;
public static final int STARTING_DIGIT_OF_TRI_NUM =0;
public static final int STAR_MULIPLIER = 6;
public static final int STAR_INDEX_SUB = 1;
public static final int STAR_INDEX_CONSTANT = 1;
public static final int MAXIMUM_VALUE_OF_INDEX =Integer.MAX_VALUE ;
public static final int MAX_ACCEPTABLE_TRINUMBER =2147463020 ;
public static void main(String[] args) {
System.out.println("The following are Triangular Star Numbers:");
int triNumber = STARTING_DIGIT_OF_TRI_NUM;
int index = BEGINNING_VALUE;
boolean isAtMax = false;
while (index <= MAXIMUM_VALUE_OF_INDEX && !isAtMax)
{
triNumber = triNumber +index;
if( triNumber >= MAX_ACCEPTABLE_TRINUMBER )
{
isAtMax = true;
}
if(isStarNumber(triNumber))
{
System.out.println(triNumber);
}
index++;
}
System.out.println("That's all of them!");
}
public static boolean isStarNumber(int inputNum)
{
int indexCount =BEGINNING_VALUE;
int testNum =BEGINNING_VALUE;
boolean testingStars = true;
while(testingStars)
{
testNum =determineStarNumber( indexCount);
indexCount++;
if(testNum >= inputNum)
{
testingStars = false;
}
}
if(testNum == inputNum )
{
return true;
}
else
{
return false;
}
}
public static int determineStarNumber(int index)
{
int starNumber = ((STAR_MULIPLIER*(index)*(index -STAR_INDEX_SUB)) +STAR_INDEX_CONSTANT);
return starNumber;
}
}