Basicprogramming(.org) > Code and examples

One statement SIN table

(1/9) > >>

B+:
Tomaaz
--- Quote ---The whole challenge is completely pointless. What about something like this:

Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.

Perl
Code:

print for (map {sin() (1..100)})


How many other languages can do it?
--- End quote ---


--- Code: ---'one statement sin table.bas SmallBASIC 0.12.6 [B+=MGA] 2016-06-19

'using variables allows going beyond integers on one statement SIN table

for i in seq(0, 2*pi, 360/15+1) do ? round(deg(i)), ((10000*sin(i))\1)*.0001

--- End code ---

B+:
New and improved:

--- Code: ---for i in seq(0,2*pi,360/15+1) do ?round(deg(i)),round(sin(i),4)
--- End code ---

Round has decimal place option.

Thanks jsalia

B+:
Well heck, here is a trig table:

--- Code: ---'one statement trig table.bas SmallBASIC 0.12.6 [B+=MGA] 2016-06-19

'using variables allows going beyond integers on one liner trig table

for i in seq(0,2*pi,360/15+1) do ? using "##0.0000 ";deg(i),sin(i),cos(i),iff(cos(i)<>0,tan(i),999.9999)
--- End code ---

Peter:

--- Quote ---Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.

--- End quote ---

This one was hard to swallow. But very interesting.

My approach is by using direct memory operations. First we have to find a suitable memory address. This is the hardest part, because usually, a memory address resides in a variable.

The trick is using a memory address to a function which always exists. In BaCon, code is being translated to C, and therefore, there always is a function called 'main'.

We can refer to its memory address by using '&main'. Note that the construct '&main' does not refer to a variable, but to the address of a function with the actual name 'main'.

The below program simply refers to the memory address of the 'main' function to POKE and PEEK values. Whatever was there, at the point of the first POKE, we've already past it.

Regards
Peter


--- Code: ---' First we need to unprotect the memory where this program is located
PRAGMA INCLUDE <sys/mman.h>

CALL mprotect(&main-MOD(&main, getpagesize()), getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC)

' Initialize the memory address of function 'main' to 1
POKE &main, 1

' Loop until we have reached 100
REPEAT

    PRINT SIN(RAD(PEEK(&main)))
    POKE &main, PEEK(&main)+1

UNTIL PEEK(&main) = 100

--- End code ---

Tomaaz:
Newlisp:


--- Code: ---(map println (map sin (sequence 1 1000 0.1)))

--- End code ---

Not limited to integers. For integers only, Perl is pretty unbeatable. ;)


--- Code: ---print sin for(1..1000);

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version