コード例 #1
0
  private long cyclicPolygonal(long num, HashSet<Long> types, long sum) {
    /*for(int i = 0 ; i < types.size(); i++){
         System.out.print(" ");
     }
    System.out.println(types.size() + "." + num+"-"+tempType);*/

    long endsWith = num % 100;
    // long endsWith = num / 100;
    long tempSum;
    if (types.size() == 6) {
      if (firstTwoDigits == endsWith) {
        return sum;
      }
      return 0;
    }
    if (startsWithList.containsKey(Long.valueOf(endsWith))) {
      ArrayList<PolygonalNumber> temp = startsWithList.get(Long.valueOf(endsWith));
      for (PolygonalNumber pNum : temp) {
        if (!types.contains(Long.valueOf(pNum.getType()))) {
          types.add(pNum.getType());
          tempSum = cyclicPolygonal(pNum.getNum(), types, sum + pNum.getNum());
          if (tempSum != 0) {
            return tempSum;
          }
          types.remove(pNum.getType());
        }
      }
    }
    return 0;
  }
コード例 #2
0
  public long solution1() {
    TreeSet<Long> p8 = Util.getOctagonalNumbersByLimit(LIMIT);
    trimValuesLessThan(p8, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p8, 8);

    TreeSet<Long> p7 = Util.getHeptagonalNumbersByLimit(LIMIT);
    trimValuesLessThan(p7, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p7, 7);

    TreeSet<Long> p6 = Util.getHexagonalNumbersByLimit(LIMIT);
    trimValuesLessThan(p6, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p6, 6);

    TreeSet<Long> p5 = Util.getPentagonalNumbersByLimit(LIMIT);
    trimValuesLessThan(p5, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p5, 5);

    TreeSet<Long> p4 = Util.getSquareNumbersByLimit(LIMIT);
    trimValuesLessThan(p4, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p4, 4);

    TreeSet<Long> p3 = Util.getTriangleNumbersByLimit(LIMIT);
    trimValuesLessThan(p3, TRIM_VALUE);
    populateStartsWithAndPolygonalList(p3, 3);

    long sum = 0;
    HashSet<Long> types = new HashSet<Long>();
    for (PolygonalNumber num : polygonalNumbers) {
      types.add(num.getType());
      firstTwoDigits = num.getNum() / 100;
      sum = cyclicPolygonal(num.getNum(), types, num.getNum());
      types.remove(num.getType());
      if (sum != 0) {
        break;
      }
    }

    return sum;
  }