mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

CRGreathouse 2010-12-03 21:09

[QUOTE=science_man_88;239885]I've also tried css javascript and html and c and php but only the first 4 ever made it and now I forgot most of them I should start using them lol. also ASM beginner is a understatement lol, I can't get anything I've learned from books to work but if I ever do maybe it will be worth it.[/QUOTE]

I just thought I'd give you a start. Now that you know the format you can add your own languages and whatever else you like. You can always put something like

{{mylang|PHP|rank neophyte}}
{{mylang|JavaScript|I once knew this, but not anymore.}}

if you like.

science_man_88 2010-12-03 21:11

[QUOTE=CRGreathouse;239894]I just thought I'd give you a start. Now that you know the format you can add your own languages and whatever else you like. You can always put something like

{{mylang|PHP|rank neophyte}}
{{mylang|JavaScript|I once knew this, but not anymore.}}

if you like.[/QUOTE]

then I'm idiot second class lol

CRGreathouse 2010-12-03 21:12

You can even do

{{mylang|Python|I'd love to learn this some day.}}

science_man_88 2010-12-03 21:23

[QUOTE=CRGreathouse;239898]You can even do

{{mylang|Python|I'd love to learn this some day.}}[/QUOTE]

[url]http://rosettacode.org/wiki/User:Science_man_88[/url]


{{mylangbegin}}
{{mylang|PARI/GP|intermediate}}
{{mylang|x86 assembly|beginner}}
{{mylang|HTML|idiot}}
{{mylang|PHP|can't find the ini to start}}
{{mylang|C|tried, should get a compiler again unless notepad++ will work}}
{{mylang|Javascript|forgotten}}
{{mylang|CSS|have a book on it as well as many others}}
{{mylangend}}

CRGreathouse 2010-12-03 21:36

So post it. (You might also order the languages somehow, maybe alphabetically?)

science_man_88 2010-12-03 21:55

[QUOTE=CRGreathouse;239905]So post it. (You might also order the languages somehow, maybe alphabetically?)[/QUOTE]

If I added all the ones I have book(s) on I might as well list 20-30 lol

programming books i have include:

Advanced MS-dos:the microsoft guide for assembly language and C programmers (copyright 1986)

Beginning programming all in one desk reference for dummies (copyright 2008)<- syntax basics with examples in 15 languages I think.

The art of assembly language 2nd edition ( copyright 2010)

Dynamic HTML the definitive reference (last copyright 2007)

Advanced programming in the unix environment (messed up looking for C books in windows;copyright 2003)

kar_bon 2010-12-04 04:27

[QUOTE=science_man_88;239742]on another script:

[url]http://rosettacode.org/wiki/Day_of_the_week[/url]

is in the can't be done etc.[/QUOTE]

The above from post #1936 makes me thinking, why this shouldn't be implementable in PARI.

All those tasks from [url=http://rosettacode.org/wiki/Category:PARI/GP/Omit]here[/url] are marked (by whom?) not doable in PARI but I don't think so.

I decided to install PARI/GP (I used first V2.3.4 but noticed it doesn't recognize 'my'-blocks, but 'local').
After some small examples I installed the newer V2.4.2 and begun to play around and read some docs and understanding some examples.

Now here's the solution of the above task "Day of the week":

[code]
JulianDate(YYYY,MM,DD)={

my(a=0, b=0, Y=YYYY, M=MM);

if(MM < 3, Y--; M+=12);
a=floor(Y/100);
b=2-a+floor(a/4);

if(YYYY<=1582 && MM<=10 && DD<=15, b=0);

return(floor(365.25*(Y+4716)) + floor(30.6001*(M+1)) + DD + b - 1524.5);
}

DayOfWeek(JD)={
my(DayName=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]);
return(DayName[floor(divrem(JD+0.5,7)[2])+1]);
}

Day_of_the_week={
my(year);
for(year=2008,2121,
if(DayOfWeek(JulianDate(year,12,25))=="Sunday",print(year)));
}
[/code]

The main point of start is to convert the date (year,month,day) into a Julian Day (like a counter for every day from 4716-01-01 B.C.).
The function "JulianDate" returns this day-counter from the input of year, month and day.

Next step is to determine the day of the week by getting the value JulianDate MOD 7.
This value is '0' for a Monday, '1' for a Tuesday and so on.
The function "DayOfWeek" returns the string of this value.

The third part is the task which finds the years, for which the 25. of December is a Sunday for the years from 2008 to 2121.

This is my first PARI code so perhaps someone can find any issue or could improve it.

Note:
I think, no, I know the other two tasks (Date format, Date manipulation) are also doable now.

CRGreathouse 2010-12-04 05:01

[QUOTE=kar_bon;239968]All those tasks from [url=http://rosettacode.org/wiki/Category:PARI/GP/Omit]here[/url] are marked (by whom?) not doable in PARI but I don't think so.[/QUOTE]

I populated most of that category.

The description says:[INDENT]These tasks are not generally counted toward "unimplemented", as somebody, somewhere, decided that PARI/GP was inappropriate for these tasks, or implementations of them would be unenlightening. Feel free to try your hand at them anyway.[/INDENT]For some tasks, like this one, I thought that the task was doable but would be unenlightening. (In principle, all tasks are doable: create a vector representing an assembly program, convert it to a string with Strchr, write it to a file, and execute it with system().)

But as the description says, feel free to ignore the {{omit}} and add the task -- I certainly won't mind. (Ideally, if you do add it, you would remove the {{omit|PARI/GP}} line at the bottom.)

CRGreathouse 2010-12-04 05:03

[QUOTE=science_man_88;239911]If I added all the ones I have book(s) on I might as well list 20-30 lol[/QUOTE]

List whatever you like. You might choose to limit yourself to those languages for which you pass the [url=http://rosettacode.org/wiki/FizzBuzz]FizzBuzz[/url] test.

CRGreathouse 2010-12-04 05:16

[QUOTE=kar_bon;239968]I decided to install PARI/GP (I used first V2.3.4 but noticed it doesn't recognize 'my'-blocks, but 'local').[/QUOTE]

Yes, the newer ("development") version is far better. You made the right call. :smile:

[QUOTE=kar_bon;239968][code]
JulianDate(YYYY,MM,DD)={

my(a=0, b=0, Y=YYYY, M=MM);

if(MM < 3, Y--; M+=12);
a=floor(Y/100);
b=2-a+floor(a/4);

if(YYYY<=1582 && MM<=10 && DD<=15, b=0);

return(floor(365.25*(Y+4716)) + floor(30.6001*(M+1)) + DD + b - 1524.5);
}

DayOfWeek(JD)={
my(DayName=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]);
return(DayName[floor(divrem(JD+0.5,7)[2])+1]);
}

Day_of_the_week={
my(year);
for(year=2008,2121,
if(DayOfWeek(JulianDate(year,12,25))=="Sunday",print(year)));
}
[/code][/QUOTE]

Looks good -- very good, if this is your first script. Minor stuff:[LIST][*] floor(foo/4) can be written more succinctly with the integer division operator: foo\4. This is also faster if your precision is high. (Unrelated: you can also write it as foo >> 2, but this is of course only for powers of two.)[*] You can leave a value on the last line of a script (no ;) and the function will return it.*[*] I'm not sure why you write floor(divrem(JD+0.5,7)[2]); I would write round(JD)%7.[*] You shouldn't put loop variables in your my() list. This actually creates two separate values that happen to have the same value -- most confusing.[/LIST]
* You certainly don't need to, but sometimes it's nicer to write
[code]square(x)={
x^2
};[/code]
than
[code]square(x)={
return(x^2);
};[/code]

kar_bon 2010-12-04 10:21

[QUOTE=CRGreathouse;239973][LIST][*] floor(foo/4) can be written more succinctly with the integer division operator: foo\4. This is also faster if your precision is high. (Unrelated: you can also write it as foo >> 2, but this is of course only for powers of two.)[*] You can leave a value on the last line of a script (no ;) and the function will return it.*[*] I'm not sure why you write floor(divrem(JD+0.5,7)[2]); I would write round(JD)%7.[*] You shouldn't put loop variables in your my() list. This actually creates two separate values that happen to have the same value -- most confusing.[/LIST][/QUOTE]

Thanks for the notes.
I've not yet read much about the special cases of PARI, so for example the 'return' I did is more from others languages they do this.
The "foo>>2" shift (like a division by a power of 2) is like in C but I don't want to use it to be more closer to the algorithm of the calculation of the Julian Date.
The round-part was not in my mind... I have to develop more in PARI first to know all basic parts and how to use.

Here's the code with your hints and suggestions:
[code]
JulianDate(YYYY,MM,DD)={
my(a=0, b=0, Y=YYYY, M=MM);
if(MM<3, Y--; M+=12);
a=Y\100;
b=2-a+a\4;
if(YYYY<=1582 && MM<=10 && DD<=15, b=0);
floor(365.25*(Y+4716)) + floor(30.6001*(M+1)) + DD + b - 1524.5
};

DayOfWeek(JD)={
my(DayName=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]);
DayName[round(JD)%7+1]
};

Day_of_the_week={
for(year=2008,2121,
if(DayOfWeek(JulianDate(year,12,25))=="Sunday",print(year)));
}
[/code]

Please feel free to insert this code in the related page and perhaps the other two tasks.


All times are UTC. The time now is 23:07.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.