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-04 15:11 ID:Heaven

n & 1 ? odd : even

bye

Name: Anonymous 2007-09-04 15:12 ID:Heaven

gb2/homeworkhelp/

Name: age 2007-09-04 15:14 ID:bG0CVxDp

#fortune

Name: Anonymous 2007-09-04 15:14 ID:Heaven

HIGHSCHOOL LEVEL QUESTION HERE:

Under what conditions does the sequence terminate? Because I, as an underage faggot, think that the sequence in question is infinite.

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

>>2
is probably stoned and not able to comprehend what i wrote

Name: Anonymous 2007-09-04 15:16 ID:TY9/U1TI

>>5
It doesn't.

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

>>5
the sequence terminates at 1

Name: Anonymous 2007-09-04 15:18 ID:J+sT6QT8

LOL I WONDER WHERE THIS IS FROM. I'VE NEVER HEARD OF THIS TRIVIAL PROBLEM BEFORE!

http://projecteuler.net/index.php?section=view&id=14

Name: sage 2007-09-04 15:18 ID:Heaven

>>9
also, forgot my sage

Name: Anonymous 2007-09-04 15:18 ID:Heaven

>>7
So every sequence, with the starting (natural) number below or not one million, is infinite. What.

Name: Anonymous 2007-09-04 15:19 ID:Heaven

Disregard >>11, thanks >>8

Name: Anonymous 2007-09-04 15:20 ID:Heaven

Next up: a unique challenge. Please.

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

BTW: my program just shits values all over the place and has a runtime of 1:21, thats longer than the time i needed to write it.

Name: Anonymous 2007-09-04 15:37 ID:Heaven

>>6

if(n == 1) stop;

if(n & 1) n /= 2;
else n = n * 3 + 1;
repeat;

some shit like this?

Name: Anonymous 2007-09-04 16:14 ID:bG0CVxDp

Gogo, poast your solutions, expert programmers.

Name: Anonymous 2007-09-04 16:21 ID:gLr9AJea

Here's my solution, in pseudocode:

PostChallengeAtWorld4ch()
WaitForMultipleAnonymous()
CopyPastaSolution()

Name: Anonymous 2007-09-04 17:31 ID:Heaven

For extra credit, prove that the sequence will terminate for any n. You should spend 5 minutes on this problem.

Name: Anonymous 2007-09-04 19:14 ID:8WJGrSko

(define (longest-sequence n)
  (define length-seq
    (memoize (lambda (x)
               (cond ((= x 1) 0)
                     ((even? x) (+ (length-seq (/ x 2)) 1))
                     (else (+ (length-seq (+ (* 3 x) 1)) 1))))))
  (define (find-longest i start length)
    (cond ((>= i n) (display `(longest sequence starts with ,start and has length ,length)))
          ((> (length-seq i) length) (find-longest (+ i 1) i (length-seq i)))
          (else (find-longest (+ i 1) start length))))
  (find-longest 1 0 0))

Name: Anonymous 2007-09-04 19:16 ID:ez68s2X/

tl;dr read sicp

Name: Anonymous 2007-09-04 19:40 ID:EZ4jcteU

I think the implication is that the sequence terminates when it starts to loop back to numbers it's already had.

For example 1 -> 4 -> 2 is a sequence (as then 2 -> 1 again)

Some interesting characteristics of this problem:

- if your sequence hits a power of two it will inevitably end up at 1, which then terminates the sequence
- an odd number is always followed by an even number ( 3n + 1 => odd * odd + odd = odd + odd = even )

Name: Anonymous 2007-09-05 3:56 ID:lmR0Mh+D

The answer is 837799 for one and 8400511 for ten million.

Name: Anonymous 2007-09-05 4:13 ID:rABwucbx

memo = {}

def seq(n):
    m, i = n, 0
    while m != 1:
        if m in memo:
            i += memo[m]
            m = 1
        else:
            if m%2 == 0:
                m /= 2
            else:
                m = 3*m+1
            i += 1
    memo[n] = i
    return i

maximum, max_x = 0, None

for x in range(1, 10**6):
    length = seq(x)
    if length > maximum:
        maximum, max_x = length, x

print max_x

Name: Anonymous 2007-09-05 5:19 ID:7QgTXC0R

1. you don't have to test a number if it's a member of a sequence you've already tested.

2. when a sequence reaches a power of two, the number of bits in the binary representation of that number plus the sequence 1, 4, 2, 1 is the number of terms left in the sequence.

3. A number which is two raised to an odd power can only appear at the start of a sequence, hence odd powers of two don't need to be tested since we can just set the initial known maximum sequence length to 24.

I think we'll all agree that these are all pretty trivial points, the thing that bothers me is that noone even tried to implement them (except 1).

Name: Anonymous 2007-09-05 5:44 ID:rABwucbx

>>24
I did #1 and it was already fast enough.

Name: Anonymous 2007-09-05 6:08 ID:Heaven

>>24
Besides, considering the abundance of powers of two under one million, points #2 and #3 would be a massive waste of time, even if implementing them took only a few minutes. And they would needlessly clutter the code.

Name: Anonymous 2007-09-05 6:32 ID:N3V2WcCD

>>24
Your second point is incorrect. Power-of-two sequences, starting at 20:

20 1->4->2 (length 3)
21 2->1->4 (length 3)
22 4->2->1 (length 3)
23 8->4->2->1 (length 4)
24 16->8->4->2->1 (length 5)
..etc..

i.e. sequence length starting at 2n is 3 when 0 < n < 2 and n + 1 when n >= 2

Name: Anonymous 2007-09-05 6:36 ID:nfZ5LK9h

1←2←4←8←16←32←64←...
2←3←6←12←24←48←...
3←6←...
4←9←18←36←...

5 ?????
WHERE THE FUCK IS 5?????

Name: Anonymous 2007-09-05 6:45 ID:N3V2WcCD

>>28
What the fuck are you on about?

Name: Anonymous 2007-09-05 6:54 ID:IgJ3uvRL

1<-2<-4<-8<-16<-5

and five there is!

Name: Anonymous 2007-09-05 7:37 ID:N3V2WcCD

I haven't written anything in Java for over five years, but I wanted to use the arbitrary precision integer library. I forgot how disgustingly verbose it is.

I assumed that the only cycle present is 1 -> 2 -> 4, and special cased those three starting positions to 3 (though I needn't have done so for 4). Everything else therefore must terminate at 1.

Well, here is the code anyway:

import java.math.BigInteger;

public class World4ChProgPuzzle {

  private static int calculateSequenceLength(int start)
  {
    switch(start)
    {
    case 1:
    case 2:
    case 4:
      return 3;
    default:
      {
        int Length = 1;
        BigInteger n = BigInteger.valueOf(start);
        while (!n.equals(BigInteger.ONE))
        {
      if (n.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO))
          {
            n = n.divide(BigInteger.valueOf(2));
          }
          else
          {
            n = n.multiply(BigInteger.valueOf(3)).add(BigInteger.valueOf(1));
          }
          ++Length;
        }
        return Length;
      }
    }
  }

  public static void main(String[] args)
  {
       
    int UpperLimit = 1000000;
    int LongestSequenceLength = 0;
    int CurrentSequenceLength;

    for (int n=1; n<=UpperLimit; n++)
    {
      CurrentSequenceLength = calculateSequenceLength(n);
      LongestSequenceLength = Math.max(LongestSequenceLength, CurrentSequenceLength);
    }
       
    System.out.println("Longest sequence for numbers up to " + UpperLimit + " is " +

LongestSequenceLength);

  }

}


And the output:

Longest sequence for numbers up to 1000000 is 525

Name: Anonymous 2007-09-05 7:51 ID:N3V2WcCD

Also, longest sequence from first ten million starting points is 686.

Name: Anonymous 2007-09-05 7:54 ID:Heaven

>>28
You're doing it wrong.

Name: Anonymous 2007-09-05 8:14 ID:TYgeAxik

ur doin it ron

Name: Anonymous 2007-09-05 8:16 ID:nfZ5LK9h

1←2←4←8←16←32←64←...
2←3←6←12←24←48←...
3←6←...
4←9←18←36←...

5 ?????
WHERE THE FUCK IS 5?????

Name: Anonymous 2007-09-05 8:19 ID:Heaven

>>31
You must be doing something horribly wrong besides being ENTERPRISE.

$ time java World4ChProgPuzzle > /dev/null
real    [b]6m15.285s[/b]
user    4m35.671s
sys    0m15.953s

Name: Anonymous 2007-09-05 8:26 ID:nfZ5LK9h

>>36
[b]lol[/b]

Name: Anonymous 2007-09-05 8:31 ID:lmR0Mh+D

Bruteforce, enterprise style.

$ time java IterativeSeq
Nr: 837799, length:525

real    0m42.984s
user    0m40.943s
sys     0m1.420s

Name: Anonymous 2007-09-05 8:40 ID:Heaven

>>36
Time-space tradeoff. If I cached the results of earlier calculations it would (often) be quicker to look them up rather than recalculate so many times. But as the bruteforce program took only a minute to run on my PC, I would surely be making unnecessary optimizations.

Name: Anonymous 2007-09-05 8:49 ID:Heaven

>>39
That hardly explains it. If we disable the memoization in the pythonfag's example, we get:

$ time python 23.py > /dev/null
real    2m40.108s
user    1m55.251s
sys    0m1.248s


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