Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.Paths;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Degrees {
  7. public static ArrayList<Long> triangularNumbers = new ArrayList<>();
  8. public static boolean isAwesome(double num){
  9. for(int i=1; i<triangularNumbers.size(); i++){
  10. for(int j=1; j<triangularNumbers.size(); j++){
  11. if(triangularNumbers.get(i) + triangularNumbers.get(j) == num && i!=j)
  12. return true;
  13. else if(triangularNumbers.get(i) + triangularNumbers.get(j)>num){
  14. break;
  15. }
  16. }
  17. }
  18. return false;
  19. }
  20. // found on internet, tested extensively, found to be correct
  21. public static boolean isTriangular(double n){
  22. long calc_num = (long) (8*n+1);
  23. long t = (long) Math.sqrt(calc_num);
  24. if (t*t==calc_num) {
  25. return true;
  26. }
  27. return false;
  28. }
  29.  
  30. public static void main(String[] args) throws IOException {
  31. Scanner in = new Scanner(Paths.get("C:\\Users\\Gates Wang\\Desktop\\degrees.txt"));
  32. int count = 0;
  33.  
  34. for(int i=0; i<5050813; i++){ //largest number is 5050813, therefore all triangular numbers must be less than 5050813
  35. if(isTriangular(i)){
  36. triangularNumbers.add((long)i);
  37. }
  38. }
  39. while(in.hasNext()){
  40. double num = in.nextDouble();
  41. if(isAwesome(num))
  42. count++;
  43. }
  44. System.out.println(count);
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement