Created
January 6, 2019 12:42
-
-
Save ClearNB/7f1f7bb2d33e4957a16cd146fb2725f8 to your computer and use it in GitHub Desktop.
Highly divisible triangular number (Java 8 ver) Sample by ClearNB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Solution { | |
static int factor(int a) { | |
int count = 0; | |
if(a == 1) { | |
return 1; | |
} | |
for(int i = 1; i < Math.ceil(Math.sqrt(a)); i++) { | |
if((a % i) == 0) { | |
count += 2; | |
} | |
} | |
if((Math.ceil(Math.sqrt(a))) == Math.floor(Math.sqrt(a))) { | |
count++; | |
} | |
return count; | |
} | |
public static void main(String[] args) { | |
int[] cdata = new int[1001]; | |
int temp = 0; | |
int box = 0; | |
for(int i = 1; i < cdata.length; i++) { | |
while(temp <= i) { | |
box++; | |
temp = factor((box * (box + 1)) / 2); | |
} | |
cdata[i] = (box * (box + 1)) / 2; | |
} | |
try (Scanner sc = new Scanner(System.in)) { | |
int t = sc.nextInt(); | |
for(int a0 = 0; a0 < t; a0++) { | |
int n = sc.nextInt(); | |
System.out.println(cdata[n]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment