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)

science_man_88 2010-12-04 12:54

[QUOTE=kar_bon;239982]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.[/QUOTE]

the date of the week one I said was important as it looks as though
[url]http://rosettacode.org/wiki/Five_weekends[/url] may need it to check.

on another topic one thing I liked to do in javascript was what I called rainbow background ( pretty much settimeouts() and color changes going through the rainbow for the background color of the page (you could do this with frames of a movie I think for background image), for the first one is there anything like this in PARI best I can think of is like a rate counter but with a default color change as the code), with Javscript you could turn the effect all the way to 1 ms per color ( bound to give someone epileptic shock))

science_man_88 2010-12-04 13:53

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

"partially filled-in normal 9x9 Sudoku grid"

But it looks as though you make one to start as it doesn't give one ! random(9) anyone lol just have to check i'th row the row the number is in and a 3 by 3 box that is the hard part. I wanted to make matrix(3*matrix(3,3),3*matrix(3,3)) but it doesn't work lol.

science_man_88 2010-12-04 20:25

[CODE]{for(n=1,100,
print(if(n%3,
if(n%5,
n
,
"Buzz"
)
,
if(n%5,
"Fizz"
,
"FizzBuzz"
)
))
)}[/CODE]

It appears to work but I'm stumped , looks to me like this should print n if n%15,buzz if %3, fizz if %5, ans fizzbuzz for anything else. I must be forgetting syntax, never mind i understand lol because %x is the same as writing y%x!=0 ?

CRGreathouse 2010-12-04 23:09

Now that I think about it, you could even do
[CODE]DayOfWeek(JD)={
["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][round(JD)%7+1]
};[/CODE]
if desired,

science_man_88 2010-12-04 23:13

[CODE]isnumerical(string)=if(type(string)==t_INT || type(string)==t_REAL || type(string)==t_FRAC || type(string)==t_COMPLEX,return(1),return(0))[/CODE]

is as much as I can figure out for:

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

CRGreathouse 2010-12-05 01:27

[QUOTE=science_man_88;240046][CODE]isnumerical(string)=if(type(string)==t_INT || type(string)==t_REAL || type(string)==t_FRAC || type(string)==t_COMPLEX,return(1),return(0))[/CODE][/QUOTE]

You need quotes. Also, no need for if(a==b || c == d, 1, 0), just do a==b || c==d:
[CODE]isnumerical(string)=my(t=type(string));t=="t_INT" || t=="t_REAL" || t=="t_FRAC" || t=="t_COMPLEX";[/CODE]

[QUOTE=science_man_88;240046]is as much as I can figure out for:

[url]http://rosettacode.org/wiki/Determine_if_a_string_is_numeric[/url][/QUOTE]

Well, your code doesn't quite answer the question. It's saying: given a string (so type(string) == "t_STR"), does it represent a number?

For example, isNumericString("123") should return 1, while isNumericString("1+1") and isNumericString("foo") should return 0.

science_man_88 2010-12-05 01:30

[QUOTE=CRGreathouse;240055]You need quotes. Also, no need for if(a==b || c == d, 1, 0), just do a==b || c==d:
[CODE]isnumerical(string)=my(t=type(string));t=="t_INT" || t=="t_REAL" || t=="t_FRAC" || t=="t_COMPLEX";[/CODE]



Well, your code doesn't quite answer the question. It's saying: given a string (so type(string) == "t_STR"), does it represent a number?

For example, isNumericString("123") should return 1, while isNumericString("1+1") and isNumericString("foo") should return 0.[/QUOTE]

I have another method but it won't work for anything but integers, so it's also useless.

I've tried every combo of Vec string and eval I can think of to try and take it out of string form but I can't make it work.

CRGreathouse 2010-12-05 01:37

[QUOTE=science_man_88;240056]I've tried every combo of Vec string and eval I can think of to try and take it out of string form but I can't make it work.[/QUOTE]

eval is the wrong tool, since type(eval("1+1")) == "t_INT". You need to go character-by-character and see if all characters are digits (and that there's at least one digit). This will check if the string represents an integer. Oh, you also need to allow either a single - or a single + as the first character.

You then also need to check for real numbers, which may be of the form
(integer).
(integer).(+integer)
.(+integer)
(integer)e(integer)
(integer).e(integer)
(integer).(+integer)e(integer)
.(+integer)e(integer)

Oh yes, and the two forms starting with . can also have a + or - prepended. And the e can be capital.

You could also check for rationals and complex numbers, but you could probably submit without.

science_man_88 2010-12-05 01:41

[QUOTE=CRGreathouse;240058]eval is the wrong tool, since type(eval("1+1")) == "t_INT". You need to go character-by-character and see if all characters are digits (and that there's at least one digit). This will check if the string represents an integer. Oh, you also need to allow either a single - or a single + as the first character.

You then also need to check for real numbers, which may be of the form
(integer).
(integer).(+integer)
.(+integer)
(integer)e(integer)
(integer).e(integer)
(integer).(+integer)e(integer)
.(+integer)e(integer)

Oh yes, and the two forms starting with . can also have a + or - prepended. And the e can be capital.

You could also check for rationals and complex numbers, but you could probably submit without.[/QUOTE]

what if they give 2^p-1 ? is that counted as complex with eval ?

CRGreathouse 2010-12-05 01:44

[QUOTE=science_man_88;240059]what if they give 2^p-1 ? is that counted as complex with eval ?[/QUOTE]

If you are passed the string "2^p-1" then the answer should be 0.

science_man_88 2010-12-05 01:46

[QUOTE=CRGreathouse;240061]If you are passed the string "2^p-1" then the answer should be 0.[/QUOTE]

This sounds too hard, too many things to look for.


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

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