Author Topic: One statement SIN table  (Read 12658 times)

B+

  • Guest
One statement SIN table
« on: June 19, 2016, 06:50:57 PM »
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: [Select]

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


How many other languages can do it?

Code: [Select]
'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

B+

  • Guest
Re: One statement SIN table
« Reply #1 on: June 19, 2016, 11:32:52 PM »
New and improved:
Code: [Select]
for i in seq(0,2*pi,360/15+1) do ?round(deg(i)),round(sin(i),4)
Round has decimal place option.

Thanks jsalia

B+

  • Guest
Re: One statement SIN table
« Reply #2 on: June 20, 2016, 01:39:00 AM »
Well heck, here is a trig table:
Code: [Select]
'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)

Peter

  • Guest
Re: One statement SIN table
« Reply #3 on: September 22, 2016, 07:16:56 PM »
Quote
Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.

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: [Select]
' 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
« Last Edit: September 23, 2016, 02:23:02 PM by Peter »

Tomaaz

  • Guest
Re: One statement SIN table
« Reply #4 on: September 22, 2016, 10:11:29 PM »
Newlisp:

Code: [Select]
(map println (map sin (sequence 1 1000 0.1)))

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

Code: [Select]
print sin for(1..1000);

Peter

  • Guest
Re: One statement SIN table
« Reply #5 on: September 23, 2016, 06:21:47 AM »
In BaCon I can do floating values too, see below for the same range from 1-1000 step 0.1. But my code isn't getting any smaller... ;)

Code: [Select]
' 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)

' Set the way we look at memory
OPTION MEMTYPE float

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

' Loop until we have reached 1000
REPEAT

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

UNTIL PEEK(&main) >= 1000
« Last Edit: September 23, 2016, 02:22:40 PM by Peter »

Aurel

  • Guest
Re: One statement SIN table
« Reply #6 on: September 23, 2016, 08:45:13 AM »
ok
print sin for(1..1000);

is that work without two points ?
(1..1000)

i think not ..so this is just ordinary predefined for-Loop

Peter

  • Guest
Re: One statement SIN table
« Reply #7 on: September 23, 2016, 09:10:39 AM »
Quote
For integers only, Perl is pretty unbeatable.

Well, it turns out that the 'map' function in Perl uses an anonymous variable behind the scenes.

So in the end, it uses a variable after all, we could consider this cheating....?  :)

Tomaaz

  • Guest
Re: One statement SIN table
« Reply #8 on: September 23, 2016, 10:04:03 AM »
Well, it turns out that the 'map' function in Perl uses an anonymous variable behind the scenes.

So in the end, it uses a variable after all, we could consider this cheating....?  :)

print sin for(1..1000) doesn't use map. ;) But, you're right - it uses the $_ variable which is used by default (you don't need to type it). And the original task was Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name. ;) To be honest I posted it as an example of a task that is to specific and constructed the way that makes certain language (Perl in this case) an automatic winner. But, was I right? I'm not sure anymore. ;)

ok
print sin for(1..1000);

is that work without two points ?
(1..1000)

i think not ..so this is just ordinary predefined for-Loop

Yes, it is. What's wrong with that? Did anyone say that loops were not allowed? :) (1..1000) is a range of integers between 1 and 1000. You could also write it like that:

Code: [Select]
for [$_] (1..1000) {
    print sin([$_]);
}

$_ is used by default if the other variable name is not specified, so you don't have to write it. Unfortunately, ranges in Perl don't allow to define step, so, to get sinuses of numbers from 1 to 1000 step 0.1, you would have to write something like this:

Code: [Select]
print sin for (map {$_ / 100} (1..100000))

I thik that in this case Newlisp wins. ;)

Okay, I get it now. SIN() range with no variables used.

Have fun!

Any task that ScriptBasic fail to complete is automatically a stupid one? ;)
« Last Edit: September 23, 2016, 10:06:33 AM by Tomaaz »

B+

  • Guest
Re: One statement SIN table
« Reply #9 on: September 23, 2016, 02:46:52 PM »
For Tomaaz original challenge, I gave up doing it without a variable and was happy to be able to do a whole trig table in one statement (not one line with : separating statements, just one statement) which so happens to be the title of this thread. "One statement SIN table"

I tried to print out a more meaningful table incremented by fractions of circle instead of Radian values incremented by 1,

 because I realized Tomaaz would be harder put trying to do it without integers but apparently he found something for that too!  ;)   (but with pi?)

Ha, I never expected Peter to focus on the the no variable part! Whew!!! awesome.

As John says, "Have fun!"
« Last Edit: September 23, 2016, 03:02:35 PM by B+ »

Peter

  • Guest
Re: One statement SIN table
« Reply #10 on: September 23, 2016, 03:09:20 PM »
There is even a more simple way to achieve this goal by using the TIMER function. This function keeps track of the amount of milliseconds a program is running. If your favorite BASIC has a similar function then it should work there too.

Code: [Select]
REPEAT

    SLEEP 10
    PRINT SIN(RAD(TIMER/10))

UNTIL TIMER/10 > 100

PS here TIMER is not a variable. Note that in BaCon, functions without arguments are written without parenthesis. So it is TIMER and not TIMER().

Tomaaz

  • Guest
Re: One statement SIN table
« Reply #11 on: September 23, 2016, 03:55:00 PM »
There is even a more simple way to achieve this goal by using the TIMER function.

Wow! This is really cool! 8)

ScriptBasic

  • Guest
Re: One statement SIN table
« Reply #12 on: September 23, 2016, 09:55:17 PM »
Is one line being defined by a line ending character? If that's the case, jQuery is a one line library.

B+

  • Guest
Re: One statement SIN table
« Reply #13 on: September 23, 2016, 11:58:30 PM »
https://en.wikipedia.org/wiki/Statement_(computer_science)
Quote
In computer programming, a statement is the smallest standalone element of an imperative programming language that expresses some action to be carried out. It is an instruction written in a high-level language that commands the computer to perform a specified action.[1] A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g., expressions).

Many languages (e.g. C) make a distinction between statements and definitions, with a statement only containing executable code and a definition instantiating an identifier, while an expression evaluates to a value only. A distinction can also be made between simple and compound statements; the latter may contain

If a function is considered just a definition, then it is a piece of cake to write a one statement program to produce a sin table. But I am pretty sure definitions use variables.
« Last Edit: September 24, 2016, 12:01:26 AM by B+ »

B+

  • Guest
Re: One statement SIN table
« Reply #14 on: September 24, 2016, 12:35:03 AM »
 :D :D :D

Here it is! no variables, one statement on what SmallBASIC thinks is one line:
Code: [Select]
? "0 0" + chr(10) + "1 0.8414709848079" + chr(10) + "2 0.90929742682568" + chr(10) + "3 0.14112000805987" + chr(10) + "4 -0.75680249530793" + chr(10) + "5 -0.95892427466314" + chr(10) + "6 -0.27941549819893" + chr(10) + "7 0.65698659871879" + chr(10) + "8 0.98935824662338" + chr(10) + "9 0.41211848524176" + chr(10) + "10 -0.54402111088937" + chr(10) + "11 -0.9999902065507" + chr(10) + "12 -0.53657291800043" + chr(10) + "13 0.42016703682664" + chr(10) + "14 0.99060735569487" + chr(10) + "15 0.65028784015712" + chr(10) + "16 -0.28790331666507" + chr(10) + "17 -0.96139749187956" + chr(10) + "18 -0.75098724677168" + chr(10) + "19 0.14987720966295" + chr(10) + "20 0.91294525072763" + chr(10) + "21 0.83665563853606" + chr(10) + "22 -0.0088513092904" + chr(10) + "23 -0.84622040417517" + chr(10) + "24 -0.90557836200662" + chr(10) + "25 -0.13235175009777" + chr(10) + "26 0.7625584504796" + chr(10) + "27 0.9563759284045" + chr(10) + "28 0.27090578830787" + chr(10) + "29 -0.66363388421297" + chr(10) + "30 -0.98803162409286" + chr(10) + "31 -0.40403764532307" + chr(10) + "32 0.55142668124169" + chr(10) + "33 0.99991186010727" + chr(10) + "34 0.52908268612002" + chr(10) + "35 -0.42818266949615" + chr(10) + "36 -0.99177885344312" + chr(10) + "37 -0.643538133357" + chr(10) + "38 0.29636857870939" + chr(10) + "39 0.96379538628409" + chr(10) + "40 0.74511316047935" + chr(10) + "41 -0.15862266880471" + chr(10) + "42 -0.91652154791563" + chr(10) + "43 -0.8317747426286" + chr(10) + "44 0.01770192510541" + chr(10) + "45 0.85090352453412" + chr(10) + "46 0.90178834764881" + chr(10) + "47 0.12357312274522" + chr(10) + "48 -0.76825466132367" + chr(10) + "49 -0.95375265275947" + chr(10) + "50 -0.26237485370393" + chr(10) + "51 0.67022917584337" + chr(10) + "52 0.98662759204049" + chr(10) + "53 0.39592515018183" + chr(10) + "54 -0.55878904885162" + chr(10) + "55 -0.99975517335862" + chr(10) + "56 -0.52155100208691" + chr(10) + "57 0.43616475524782" + chr(10) + "58 0.99287264808454" + chr(10) + "59 0.63673800713914" + chr(10) + "60 -0.30481062110222" + chr(10) + "61 -0.96611777000839" + chr(10) + "62 -0.73918069664922" + chr(10) + "63 0.16735570030281" + chr(10) + "64 0.92002603819679" + chr(10) + "65 0.8268286794901" + chr(10) + "66 -0.02655115402397" + chr(10) + "67 -0.85551997897532" + chr(10) + "68 -0.89792768068929" + chr(10) + "69 -0.11478481378319" + chr(10) + "70 0.77389068155789" + chr(10) + "71 0.95105465325437" + chr(10) + "72 0.25382336276204" + chr(10) + "73 -0.67677195688731" + chr(10) + "74 -0.98514626046825" + chr(10) + "75 -0.38778163540943" + chr(10) + "76 0.56610763689818" + chr(10) + "77 0.99952015858073" + chr(10) + "78 0.51397845598754" + chr(10) + "79 -0.44411266870751" + chr(10) + "80 -0.99388865392338" + chr(10) + "81 -0.62988799427445" + chr(10) + "82 0.31322878243309" + chr(10) + "83 0.96836446110019" + chr(10) + "84 0.73319032007329" + chr(10) + "85 -0.17607561994859" + chr(10) + "86 -0.92345844700406" + chr(10) + "87 -0.82181783663082" + chr(10) + "88 0.03539830273366" + chr(10) + "89 0.86006940581245" + chr(10) + "90 0.89399666360056" + chr(10) + "91 0.10598751175116" + chr(10) + "92 -0.7794660696158" + chr(10) + "93 -0.94828214126995" + chr(10) + "94 -0.24525198546765" + chr(10) + "95 0.68326171473612" + chr(10) + "96 0.98358774543434" + chr(10) + "97 0.37960773902752" + chr(10) + "98 -0.57338187199042" + chr(10) + "99 -0.99920683418635" + chr(10) + "100 -0.50636564110976"
Here is the program that made the program:
Code: [Select]
' make sin table program.bas for SmallBASIC 0.12.7 [B+=MGA] 2016-09-23

t = "? "
for i = 0 to 100
  if i < 100 then
    t = t + enclose(str(i) + " " + sin(i)) + " + chr(10) + "
  else
    t = t + enclose(str(i) + " " + sin(i))
  fi
next
tsave "Sin table program.bas", t
« Last Edit: September 24, 2016, 01:04:00 AM by B+ »