User:Yeti: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Blanked the page)
 
Line 1: Line 1:
=AWK/Primes/FSOE=

==AWK/Primes/FSOE1==
FSOE1 needs a number:set mapping and expressing this in AWK would blow up the code too much. (Or shorter: Maybe later.)
==AWK/Primes/FSOE3==
<lang awk>BEGIN {
n=2
while( n<100 ) {
if( n in L ) {
p=L[n]
del L[n]
} else {
p=n
printf n " "
}
npp=n+p
while( npp in L )
npp=npp+p
L[npp]=p
n++
}
print
}</lang>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
</pre>



=BASH/Self Modyfying Code=
<pre>
$ . Thing-1033909978
$ type Thing
Thing is a function
Thing ()
{
local get='shift;local __=$1;shift;echo ${!__}' set='shift;__=$1;shift;local $__="$*";eval "$FUNCNAME () { local $(local | tr "\n" " "); $(type $FUNCNAME | tail +5)"' version=1033909978;
eval ${!1}
}
$ Thing get version
1033909978
$ date -d@$(Thing get version) # ;-)
Sun Oct 6 13:12:58 UTC 2002
$ ##
$ ## add a new property 'show'
$ ##
$ Thing set show local
$ ##
$ ## try it
$ ##
$ Thing show
get='shift;local __=$1;shift;echo ${!__}'
set='shift;__=$1;shift;local $__="$*";eval "$FUNCNAME () { local $(local | tr "\n" " "); $(type $FUNCNAME | tail +5)"'
show=local
version=1033909978
</pre>

=Dc/Questions/DROP=
TODO Drop top of stack without side effects
<pre>
# 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
</pre>
{{out}}
<pre>
Stack:
gold
</pre>
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/FSOE=
{{works with|Python|2.x}}
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.

==Python/Primes/FSOE1==

Variant: 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>

{{out}}
<pre>
2
3
5
7
11
13
17
19
23
29
</pre>
...the program has to be terminated by the user e.g. by typing ctrl-c.

==Python/Primes/FSOE3==
{{works with|Python|2.x}}

Variant: A tape position can hold one wheel.

<lang python>L = {}
n = 2

while 1:

if n in L:
P = L[n]
del L[n] # optional - saves some memory.
else:
P = n
print n

m = n+P
while m in L:
m += P
L[m] = P

n += 1</lang>
{{out}}
<pre>
2
3
5
7
11
13
17
19
23
29
</pre>
...the program has to be terminated by the user e.g. by typing ctrl-c.

----

{{mylangbegin}}
{{mylang|AWK|}}
{{mylang|B|}}
{{mylang|BASH|}}
{{mylang|C|}}
{{mylang|Dc|}}
{{mylang|Spin|}}
{{mylangend}}

Latest revision as of 09:25, 7 June 2021