Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge

Name: Anonymous 2007-09-04 15:06 ID:bG0CVxDp

Hey, I've stolen this challenge from some website, it's pretty easy but I know most of you will fuck it up.

A sequence is defined by:

n -> n/2 (n is even)
n -> 3n + 1 (n is odd)

Find the longest sequence, with a starting number under one million.

Bonus points when you find the solution for ten million.

Name: Anonymous 2007-09-05 12:41 ID:N3V2WcCD

Oh, ten million.

Ok that doesn't overflow on the brute force one with longs (and it takes 72.4 seconds), but it does cause Java to run out of heap pretty quickly on my Hashtable one. I can't seem to find the necessary combination of switches to give the JVM more memory to work in though.

Here's some more code (longs+hashtable):

import java.util.Hashtable;

public class World4ChProgPuzzleOpt2 {

  static Hashtable<Long, Integer> LengthCache = new Hashtable<Long, Integer>();

  private static int calculateSequenceLength(int start) {
    switch(start) {
    case 1:
    case 2:
    case 4:
      return 3;
    default:
      {
        int Length = 1;
        Integer CachedLength = null;
        long n = start;

        while (!(n==1||CachedLength!=null)) {
          CachedLength = LengthCache.get(n);
          if (CachedLength != null) {
            Length = Length + CachedLength - 1;
          } else {
            if (n%2==0) {
              n /= 2;
            } else {
              n = 3*n+1;
            }
            ++Length;
          }
        }
        LengthCache.put((long)start, Length);
        return Length;
      }
    }
  }

  public static void main(String[] args) {
       
    int UpperLimit = 1000000;
    int LongestSequenceLength = 0;
    int CurrentSequenceLength;
    long StartTime = System.currentTimeMillis();

    for (int n=1; n<=UpperLimit; n++) {
      CurrentSequenceLength = calculateSequenceLength(n);
      if (CurrentSequenceLength > LongestSequenceLength) {
        LongestSequenceLength = CurrentSequenceLength;
      }
    }
   
    long ElapsedTime = System.currentTimeMillis()-StartTime;

    System.out.println("Longest sequence for numbers up to " + UpperLimit + " is " + LongestSequenceLength);
    System.out.println("(took "+ElapsedTime+"ms to run)");


  }

}


Name: Anonymous 2007-09-05 12:50 ID:77Ch8IzL

>>81
Enterprise

Name: Anonymous 2007-09-05 13:53 ID:N3V2WcCD

I just realised how stupid I was using a hashtable. I mean, what the fuck? Completely retarded data structure for this application. This next one just allocates a huge array instead.

Average 0.34 seconds for one million items and 4.0 seconds for ten million. Here's the code:

public class World4ChProgPuzzleOpt3 {

  static int[] LengthCache;
  static final int UpperLimit = 1000000;

  private static int calculateSequenceLength(int start) {
    switch(start) {
    case 1:
    case 2:
    case 4:
      return 3;
    default:
      {
        int Length = 1;
        int CachedLength = 0;
        long n = start;

        while (!(n==1||CachedLength!=0)) {
          if (n<=UpperLimit) {
            CachedLength = LengthCache[(int)n-1];
          }
          if (CachedLength != 0) {
            Length = Length + CachedLength - 1;
          } else {
            if (n%2==0) {
              n /= 2;
            } else {
              n = 3*n+1;
            }
            ++Length;
          }
        }
        LengthCache[start-1] = Length;
        return Length;
      }
    }
  }

  public static void main(String[] args) {
       
    int LongestSequenceLength = 0;
    int CurrentSequenceLength;
    long StartTime = System.currentTimeMillis();

    LengthCache = new int[UpperLimit];

    for (int n=1; n<=UpperLimit; n++) {
      CurrentSequenceLength = calculateSequenceLength(n);
      if (CurrentSequenceLength > LongestSequenceLength) {
        LongestSequenceLength = CurrentSequenceLength;
      }
    }
   
    long ElapsedTime = System.currentTimeMillis()-StartTime;

    System.out.println("Longest sequence for numbers up to " + UpperLimit + " is " +

LongestSequenceLength);
    System.out.println("(took "+ElapsedTime+"ms to run)");


  }

}

Name: Anonymous 2007-09-05 13:59 ID:Heaven

>>83
HAHAHA! You were trolled by my Python dictfaggotry.

Name: Anonymous 2007-09-05 14:01 ID:N3V2WcCD

>>84
LOL, true :)

Name: Anonymous 2007-09-05 14:27 ID:lpHbmpVF

public class Prog {
    public static void main(String[] args) {
        int top = Integer.parseInt(args[0]);
        int[] m = new int[top+1];
        m[1] = 1;
        for(int i=2; i<=top; i++) {
            long j = i;
            int n = 0;
            do {
                n++;
                if((j&1) == 0) j /= 2;
                else j = j*3 + 1;
            } while(j > i);
            if(j == i) System.err.println("Infinite sequence: "+i);
            else m[i] = n + m[(int)j];
        }
        long max = 0;
        int maxi = -1;
        for(int i=1; i<=top; i++) if(m[i] > max) max = m[maxi = i];
        System.out.println(max+" @ "+maxi);
    }
}


$ time java Prog 1000000
525 @ 837799

real    0m0.389s
user    0m0.328s
sys     0m0.036s

$ time java Prog 10000000
686 @ 8400511

real    0m3.061s
user    0m2.876s
sys     0m0.084s

Name: Anonymous 2007-09-05 14:38 ID:N3V2WcCD

>>86
Interesting .. 35% faster to test & 1 rather than % 2.

Name: Anonymous 2007-09-05 14:40 ID:+nbUCTUW

>>87
Well of course.

Name: Anonymous 2007-09-05 14:42 ID:nfZ5LK9h

>>87
compiler should optimize that, failed compiler

Name: Anonymous 2007-09-05 18:28 ID:Heaven

>>89
more like fail language

Name: Anonymous 2007-12-25 20:16

And so, Ruby imitates fail.

Name: Anonymous 2007-12-25 20:54

BLAST FROM THE SAGEnoateaoeao eocioeit q;jt;q90

Name: Anonymous 2007-12-25 21:52

MATHEMATICAL INDUCTION MOTHERFUCKER, DO YOU USE IT?

Name: Anonymous 2007-12-25 23:51

SICP MOTHERFUCKER, DO YOU READ IT?

Name: Anonymous 2009-03-06 11:10

IVAN I know you   can pass functions   of course you   would turn off   active x anytime.

Name: Anonymous 2010-12-21 21:35

Name: WHERE THE FUCK IS 5????? 2011-02-10 5:24

WHERE THE FUCK IS 5?????

Name: Anonymous 2011-02-18 14:02

<-- check 'em

Name: Anonymous 2011-02-18 17:40

<-- check my doubles

Name: Anonymous 2011-09-19 6:29

pantsu

Name: Anonymous 2012-06-18 22:28

>>81
java -Xmx1024M World4ChProgPuzzleOpt2

Name: Anonymous 2012-06-18 22:30

>>1
I found a number for which the sequence diverges infinitely, what do I get?

Name: Anonymous 2012-06-18 22:55

>>103
So you've settled Collatz conjecture? I'm not impressed.

Name: Anonymous 2012-06-18 23:09

>>104
,,Conjecture''?  You mean to tell me someone actually spent more than five minutes on this?

Name: Anonymous 2012-06-18 23:15

>>102
Aha, thanks. If only you'd told me that five years ago!

Name: Anonymous 2012-06-19 11:52

南憔㍨褩ᙔ؆暖遈题劃銘䊃䀒挨ᒁ䖓爓蠸吀舧ܦ҅祗║ᕇ␰鈐疘ぢ㕣坣鉲䔘睩鄔╧頴灄蕸䢖䑷爲陦塹鍤Ԇ爁᥄ܰᑰᤧ摑蠗聇阘Ȉ煣ᔵ႗捙ক⑱瘶㔠攈ࡤ褩➇㠈ㅥ妄嚃न㝂卑ऩ逳码䁄片椐ᆃ䈸顓䥔䢙灧ᑗ瘳敡㑰蕉ᔆ愴㉨晈聓࠳⠘啳逨嘈䑗圖蘳螃啅猇硔䍱቙蝒靇礗袐Հᐱ兆⌐葘ᦓ猨饳奈㜳䒗’腁襂坁᠓ކ虖ᢑ݈ㄒцΒ⌘ᕐ┈傂焣ᥩ吗値0ႃ鞅㕦噢ɘՁ≧蒁扅畸䝦մ䕒邖•≨村䘇㥗悑材咅т暔ぉ炉眰Ƅɦ挄䘥ᘖ慠̆顕杒䝲Ⅰ奴掄ᅔ䐥䝄㕃ᔢ䎆ဩ℅极攳؆唀腡瑦颇鑈䌦❦Ԉ㠘递頂酃劄ЕŐ捤⌰♰码锃㉃⡹晑靓扄♄࠳㑘䅱搂噣䔳達蜐䡂⦓㑓䘉唆舠Ԩ〤̅ष朓熆灧聰㙶卹墁呦抓脢啗唓ᖕ䚈㜕䚘阷㍹倈ေ選圠膂萩̷錨杘阅䙹㚑⎆ᝡ剀䅸料✇㡓आ㦖ʑ䝑瀳恀掕瘇অ恄防映厘厀䦗慵≘瘅瑈㕉䔣

Name: Anonymous 2012-06-19 12:06

Ԗ♤艢杲葖鍶⊐捩᠕垂蘱㥠皔戥褧镑脷蒇⤗፷ᢄ蒒♑灓鐶┑ᅹ䢔ր蝸ѵᝣ顈⥥䥤‚膑䚙咈㍑酠े艢捂օ☘ᆘ蘢葅攱┅持ᕦ䄅爤㦃䖁圇儲椠灒摉鑳甥┱䌱妆䤧瀔蝨祤璉礢艙…瑕⍳瘳ၗ㖉㄄㍘銃啤঒鄕吧㖙掂㉆䉸眓ȨБ噤ㆀ蘡㈂腣〥Ű㆘ↄ餗ቂ衩؂ԅ㎔蠷䥔♇慴ʕ椆ᕓ䉧⑔ပ䀴餵䈘ပၶॸ䂆ࢆ癗ᠳ␦睲ᚐ性क़莃砂঑鑀兑怣蚀᎗荔刲ၨ㒗偧萳朡熇荡瘅銉ኒ冑䆓頵у桦〉⥡䝤睓夙瑅儘礢搠瑷睰፡茀䄦䍙ᤘ㌑膂文墒處ᅈИ腀ш閕晇餄ጠ䌐冃᠅У䍥艒Ȁᚉ∸̰卙逘╁頤邗鈶䘲荩啩➈偣堣ㅷ疆㊖偩妑䁲ԙ䙠堑愸荆㈈蒘鍧饩剢倳ᒈԐᢄ睔蝗ॣቡ䞙⦃ᦄ则逘嚁䝈熃䕗㢅襗吵▘垕隇馂堩’ĥ喅疃၈䐅ᄔ田升坳ᔡ怔‷蔶剥ၣ炂在抂蘉晀镲✑捀ʹᦒ昷犈搅猈ቄ瑖吰圔蔧邃搖耥傀憄覆虡朔栔镇㑸आ䘓

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List