Generator/Exponential: Difference between revisions

Content deleted Content added
→‎{{header|Perl 6}}: forgot to add output
Line 301:
'''Sample output'''
<pre>[529,576,625,676,784,841,900,961,1024,1089]</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
Generators are close to the heart and soul of Icon/Unicon. Co-expressions let us circumvent the normal backtracking mechanism and get results where we need them.
 
<lang Icon>procedure main()
 
write("Non-cube Squares (21st to 30th):")
every (k := 0, s := noncubesquares()) do
if(k +:= 1) > 30 then break
else write(20 < k," : ",s)
end
 
procedure mthpower(m) #: generate i^m for i = 0,1,...
while (/i := 0) | (i +:= 1) do suspend i^m
end
 
procedure noncubesquares() #: filter for squares that aren't cubes
cu := create mthpower(3) # co-expressions so that we can
sq := create mthpower(2) # ... get our results where we need
 
repeat {
if c === s then ( c := @cu , s := @sq )
else if s > c then c := @cu
else {
suspend s
s := @sq
}
}
end</lang>
 
Note: The task could be written without co-expressions but would be likely be ugly. If there is an elegant non-co-expression version please add it as an alternate example.
 
Output:<pre>Non-cube Squares (21st to 30th):
21 : 529
22 : 576
23 : 625
24 : 676
25 : 784
26 : 841
27 : 900
28 : 961
29 : 1024
30 : 1089</pre>
 
=={{header|J}}==