Skip to main content
golfing
Source Link

tinylisp, 993 993 823 bytes

Shout out to @DLosc for carving off 170 bytes! Woo Hoo!

(d !(q(1 1
(d B(q(S S
(d C(q((S T)(i T(C(c(a(h S)(h S))S)(s T 1))S
(d D(CF(q(1(S 1)T)30
(di ET(F(c((q((U )(a(a U U)U
(d F(q((S T)(i T(F(c(E))(h(t S)))S)(s T 1))S
(d G(F(q(1 1))! 38
(d H(q((S T)(i(e T(q(1 1))!)S(i(l(h T)(a S 1))(H(s S(h T))(t T))(H S(t T
(d I(q((S)(e 0(H S G
(d J(q((S)(e 0(H S D
(d K(q((S T)(i(e T(q(1 1))!)S(i(l(h T)(a S 1))(a(h(t(t T)))(K(s S(h T))(t T)))(K S(t T
(d L(q((S)(K S G
(d M(q((S)(N(i(Ia(H(s S 1)G)(ia(e(L(s S 1))14)(B(a SH S))(i(J(L(sC S! 130)))(B(a S S)()(B(LK(s S 1))(a S S)G)))(B(a S S
(d N(q((S T)(i S(N(t S)(c(h S)T))T
(d O(q((S T S)(i S(O(t S)(N(M(h S))T)(t S))T
(d P(q((S T S)(i(e S())T(Q(tc(h S)(cT)(ht S))T
(d Q(q((S T S)(i(e S())T(P T(t S))T
(d R(q((S T U)(i(e S())(i(e T())U(R S(t T)i(ce(h T)U))S)(i(eh T())(R(t S)(t T)(c(h S)U))(i(el(h S)(h T))(R(t S)(t T)(c(h S)U))(i(l(hR S(t T)(c(h T)U))))(R(t S)T(c(h S)U)))(i T(R S(t T)(c(h T)U))U
(d V(q((S T)(i(e S())T(V(t S)(c(h S)T))T
(d W(q((S)(i(e()S)S(i(e()(t S))S(V(R(W(Q S()S))(W(P S()S))())(
(d X(q((T)(i T(O T())(S
(d Y(q((T S)(i(e S 0)(BY(i T S)(YO(X )T)())(s S 1))(B T S
(d A(q((S)(i(e S 0)(q(1))(W(h(Y(q(B 2))(s S 1))))(B 1

Try it online!Try it online!

tinylisp, 993 bytes

(d B(q(S S
(d C(q((S T)(i T(C(c(a(h S)(h S))S)(s T 1))S
(d D(C(q(1 1))30
(d E(q((U )(a(a U U)U
(d F(q((S T)(i T(F(c(E(h(t S)))S)(s T 1))S
(d G(F(q(1 1))38
(d H(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(H(s S(h T))(t T))(H S(t T
(d I(q((S)(e 0(H S G
(d J(q((S)(e 0(H S D
(d K(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(a(h(t(t T)))(K(s S(h T))(t T)))(K S(t T
(d L(q((S)(K S G
(d M(q((S)(i(I(s S 1))(i(e(L(s S 1))1)(B(a S S))(i(J(L(s S 1)))(B(a S S))(B(L(s S 1))(a S S))))(B(a S S
(d N(q((S T)(i S(N(t S)(c(h S)T))T
(d O(q((S T)(i S(O(t S)(N(M(h S))T))T
(d P(q((S T)(i(e S())T(Q(t S)(c(h S)T
(d Q(q((S T)(i(e S())T(P(t S)T
(d R(q((S T U)(i(e S())(i(e T())U(R S(t T)(c(h T)U)))(i(e T())(R(t S)T(c(h S)U))(i(e(h S)(h T))(R(t S)(t T)(c(h S)U))(i(l(h S)(h T))(R(t S)T(c(h S)U))(R S(t T)(c(h T)U
(d V(q((S T)(i(e S())T(V(t S)(c(h S)T
(d W(q((S)(i(e()S)S(i(e()(t S))S(V(R(W(Q S()))(W(P S()))())(
(d X(q((T)(i T(O T())(
(d Y(q((T S)(i(e S 0)(B T S)(Y(X T)(s S 1
(d A(q((S)(i(e S 0)(q(1))(W(h(Y(q(2))(s S 1

Try it online!

tinylisp, 993 823 bytes

Shout out to @DLosc for carving off 170 bytes! Woo Hoo!

(d !(q(1 1
(d B(q(S S
(d C(q((S T)(i T(C(c(a(h S)(h S))S)(s T 1))S
(d F(q((S T)(i T(F(c((q((U)(a(a U U)U)))(h(t S)))S)(s T 1))S
(d G(F ! 38
(d H(q((S T)(i(e T !)S(i(l(h T)(a S 1))(H(s S(h T))(t T))(H S(t T
(d K(q((S T)(i(e T !)S(i(l(h T)(a S 1))(a(h(t(t T)))(K(s S(h T))(t T)))(K S(t T
(d M(q((S)(N(i(a(H(s S 1)G)(a(e S 4)(H S(C ! 30))))()(B(K(s S 1)G)))(B(a S S
(d N(q((S T)(i S(N(t S)(c(h S)T))T
(d O(q((T S)(i S(O(N(M(h S))T)(t S))T
(d P(q((T S)(i S(Q(c(h S)T)(t S))T
(d Q(q((T S)(i S(P T(t S))T
(d R(q((S T U)(i S(i T(i(e(h S)(h T))(R(t S)(t T)(c(h S)U))(i(l(h S)(h T))(R(t S)T(c(h S)U))(R S(t T)(c(h T)U))))(R(t S)T(c(h S)U)))(i T(R S(t T)(c(h T)U))U
(d V(q((S T)(i S(V(t S)(c(h S)T))T
(d W(q((S)(i(t S)(V(R(W(Q()S))(W(P()S))())())S
(d Y(q((T S)(i S(Y(i T(O()T)())(s S 1))(B T S
(d A(q((S)(i S(W(h(Y(B 2)(s S 1))))(B 1

Try it online!

Source Link

tinylisp, 993 bytes

(d B(q(S S
(d C(q((S T)(i T(C(c(a(h S)(h S))S)(s T 1))S
(d D(C(q(1 1))30
(d E(q((U )(a(a U U)U
(d F(q((S T)(i T(F(c(E(h(t S)))S)(s T 1))S
(d G(F(q(1 1))38
(d H(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(H(s S(h T))(t T))(H S(t T
(d I(q((S)(e 0(H S G
(d J(q((S)(e 0(H S D
(d K(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(a(h(t(t T)))(K(s S(h T))(t T)))(K S(t T
(d L(q((S)(K S G
(d M(q((S)(i(I(s S 1))(i(e(L(s S 1))1)(B(a S S))(i(J(L(s S 1)))(B(a S S))(B(L(s S 1))(a S S))))(B(a S S
(d N(q((S T)(i S(N(t S)(c(h S)T))T
(d O(q((S T)(i S(O(t S)(N(M(h S))T))T
(d P(q((S T)(i(e S())T(Q(t S)(c(h S)T
(d Q(q((S T)(i(e S())T(P(t S)T
(d R(q((S T U)(i(e S())(i(e T())U(R S(t T)(c(h T)U)))(i(e T())(R(t S)T(c(h S)U))(i(e(h S)(h T))(R(t S)(t T)(c(h S)U))(i(l(h S)(h T))(R(t S)T(c(h S)U))(R S(t T)(c(h T)U
(d V(q((S T)(i(e S())T(V(t S)(c(h S)T
(d W(q((S)(i(e()S)S(i(e()(t S))S(V(R(W(Q S()))(W(P S()))())(
(d X(q((T)(i T(O T())(
(d Y(q((T S)(i(e S 0)(B T S)(Y(X T)(s S 1
(d A(q((S)(i(e S 0)(q(1))(W(h(Y(q(2))(s S 1

Try it online!

Ungolfed:

(d list
(q(args
  args
)))


(d make-pow2
(q((S T)
  (i
    T
    (make-pow2 (c (a (h S) (h S)) S) (s T 1))
    S
  )
)))

(d pow2 (make-pow2 (q(1 1)) 30))

(d times3
(q((U )
   (a (a U U) U)
)))

(d make-pow3
(q((S T)
  (i
    T
    (make-pow3 (c (times3 (h (t S))) S) (s T 1))
    S
  )
)))

(d pow3 (make-pow3 (q (1 1)) 38))

(d trim
(q((S T)
  (i
    (e T (q(1 1)))
    S  
    (i 
      (l (h T) (a S 1))
      (trim (s S (h T)) (t T))
      (trim S (t T))
    )
  )
)))

(d div3?
(q((S)
  (e 0 (trim S pow3))
)))

(d div2?
(q((S)
  (e 0 (trim S pow2))
)))


(d div3*
(q((S T)
  (i
    (e T (q(1 1)))
    S
    (i 
      (l (h T) (a S 1))
      (a (h (t (t T))) (div3* (s S (h T)) (t T)))       
      (div3* S (t T)) 
    )
  )  
)))

(d div3
(q((S)
  (div3* S pow3)
)))




(d G
(q((S)
  (i 
    (div3? (s S 1))
    (i 
      (e (div3 (s S 1)) 1)
      (list (a S S))
      (i
        (div2? (div3 (s S 1)))
        (list (a S S))
        (list (div3 (s S 1)) (a S S))
      )      
    )
    (list (a S S))
  )
)))

(d concat
(q((S T)
  (i
    S
    (concat (t S) (c (h S) T))
    T
  )
)))

(d step*
(q((N acc)
  (i
    N
    (step* (t N) (concat (G (h N)) acc )) 
    acc
  ) 
)))

(d even
(q((S T)
  (i
    (e S ())
    T
    (odd (t S) (c (h S) T))        
  )    
)))

(d odd
(q((S T)
  (i
    (e S ())
    T
    (even (t S) T)        
  )    
)))

(d merge
(q((S T U)
  (i
    (e S ())
    (i
      (e T ())
      U
      (merge S (t T) (c (h T) U)) 
    )
    (i
      (e T ())
      (merge (t S) T (c (h S) U)) 
      (i
        (e (h S) (h T))
        (merge (t S) (t T) (c (h S) U))      
        (i
          (l (h S) (h T))
          (merge (t S) T (c (h S) U))
          (merge S (t T) (c (h T) U))
        )
      )
    )
  )  
)))

(d rev
(q((S T)
  (i 
    (e S ())
    T
    (rev (t S) (c (h S) T))
  )
)))

(d sort
(q((S)
  (i
    (e () S)
    S
    (i 
      (e () (t S))
      S
      (rev (merge (sort(odd S ())) (sort (even S ())) ()) ())
    )
  )
)))

(d step
(q((N)
  (i
    N
    (step* N ())  
    ()  
  )
)))

(d stopping
(q((N S)
  (i
    (e S 0)
    (list N S)
    (stopping (step N) (s S 1))
  )  
)))

(d A
(q((S)
  (i
    (e S 0)
    (q(1))
    (sort (h (stopping (q(2)) (s S 1))) )
  )  
)))

Try it online!

Explanation

My initial approach was a naive one where I started from 2^n and counted down checking each number to see if it's stopping number was n. That was way too slow. After consulting other answers for inspiration, I switched to using the reverse Collatz function, generating the tree, and stopping after n iterations.

I discovered by experimenting, that the tree does not grow exponentially - although n*2 is always a hit, the (n-1)/3 path only hits occasionally. So by the time we get to n=30 we have about twice the number of integers that we want, but no more than that.

As a result, I put the sort and de-dup at the very end, and called it just once - this saves some execution time.

The sort is a merge sort - this makes it convenient to de-dup while merging.

The arithmetic operations required: divides-by-two? divides-by-three? and divide-by-three all had to be optimized - the naive implementations of these were too slow. To optimize, I created a list of powers of 2 and 3, and used these to make the operations faster.

Testing

My code has been tested and verified for n = 0,1,5,9,15 and 30. All are correct. n=30 runs in about 15 seconds on TIO.