Guest User

Untitled

a guest
Jul 13th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3.  
  4. public class BruteForce {
  5.     public static final long OVERFLOW_LIMIT = (Long.MAX_VALUE -1)/3;
  6.    
  7.     public static final Long[] KNOWN_ODD_CYCLE = {1L,5L,7L,17L,25L,37L,55L,41L,61L,91L}; // Known cycle base that are odd.
  8.     public static final int MAX_K = 10000; // Max iteration we do of f before giving up and going to the next n.
  9.     public static final int LOW_BOUND_N = 100; // Lower bound of search on n.
  10.     public static final int UP_BOUND_N = 20000000; // Upper bound of search on n.
  11.    
  12.     public static void main(String[] args) {
  13.         HashSet<TripleResult> result = new HashSet<>();
  14.         final HashSet<Long> knownOddCycle = new HashSet<>(Arrays.asList(KNOWN_ODD_CYCLE));
  15.        
  16.         for ( int n = LOW_BOUND_N ; n < UP_BOUND_N ; ++n) {
  17.        
  18.             HashSet<Long> alreadySeen = new HashSet<>(); // Value we already met during iteration of f.
  19.             long x = n;
  20.             // Let's iterate !
  21.             for ( int k = 0 ; k < MAX_K ; ++k ) {
  22.                 if ( alreadySeen.contains(x) ) { // We found a cycle.
  23.                    
  24.                     if (x % 2 == 0) // Even.
  25.                         break;
  26.                     if ( knownOddCycle.contains(x)) // Already known.
  27.                         break;
  28.                     System.out.println("Found a new odd cycle starting at x=" + x +
  29.                           " with starting value n="+n + " after k="+ k + " iterations");
  30.                     result.add(new TripleResult(x, n, k));
  31.                 }
  32.                 alreadySeen.add(x);
  33.                 try {
  34.                     x = f(x); // We iterate and we catch possible integer overflow.
  35.                 } catch (Exception e) {
  36.                     System.err.println("Overflow on n=" + n + ". Will go to next n directly. " + e);
  37.                     break;
  38.                 }
  39.             }
  40.            
  41.             if ( n % 10000 == 0 )
  42.                 System.out.println("Still searching... n=" + n);
  43.            
  44.         }
  45.        
  46.         System.out.println("Finished !");
  47.         System.out.println("New results : " + result);
  48.     }
  49.    
  50.     public static long f(long x) throws Exception {
  51.         if (x % 2 == 0)
  52.             return x/2;
  53.         if ( x  > OVERFLOW_LIMIT ) { // We use 64 bits long integer. We may overflow so we must be careful.
  54.             throw new Exception("Overflow on x = " + x);
  55.         }
  56.         return 3*x-1;
  57.     }
  58.    
  59.     public static class TripleResult {
  60.         public final long x;
  61.         public final long n;
  62.         public final long k;
  63.         public TripleResult(long x, long n, long k) {
  64.             this.x = x;
  65.             this.n = n;
  66.             this.k = k;
  67.         }
  68.         @Override
  69.         public String toString() {
  70.             return "TripleResult [x=" + x + ", n=" + n + ", k=" + k + "]";
  71.         }        
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment