DC/Questions/DROP
TODO Drop top of stack without side effects
# assume there is a value in x[1337], e.g. "gold" [gold] 1337 :x # "trash" on the stack: [trash] # push x[1337] to x-stack 1337;x Sx # deposit TOS in x[1337] 1337: # top of x-stack to x[1337] Lx 1337: 1337 ;x [Stack: ]PAP f
- Output:
Stack: gold
Sigh!
Is there really no simpler way to drop the top of stack that works for strings and numbers and in all versions of Dc?
Python/Primes/FSOE1
This is a sequentialised variant of the well known sieve method (ab)using a dictionary as sparse array.
Alternatively you can describe it as finding the wheels and moving them on a tape. The only knowledge is to start looking 2 instead of starting with 1, but the original sieve does skip 1 too. 2 is not hard coded to be prime. The algorithm detects it as 1st prime and then puts the 2-wheel on the tape.
A tape position can hold multiple wheels.
<lang python>L = {} n = 2
while 1:
if n in L: P = L[n] del L[n] # optional - saves some memory. else: print n P = [n]
for p in P: npp = n+p if npp in L: L[npp].add(p) else: L[npp] = set([p])
n += 1</lang>
- Output:
2 3 5 7 11 13 17 19 23 29
...the program has to be terminated by the user e.g. by typing ctrl-c.
My Favorite Languages | |
Language | Proficiency |
AWK | --- |
C | --- |
Dc | --- |
Spin | --- |