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-02 22:30

[QUOTE=science_man_88;239747]never mind only; [url]http://rosettacode.org/wiki/Special_characters#PARI.2FGP[/url] which list 4 lol[/QUOTE]

I added those. The manual only has three, but I happen to know that \" also works.

CRGreathouse 2010-12-02 22:32

[QUOTE=science_man_88;239759]is there a way to fix pari back to installed defaults ? I played too much with things i hate the view lol.[/QUOTE]

If you want to change the window, right-click, choose properties, choose the layout tab, and change the window size and/or the screen buffer size.

CRGreathouse 2010-12-02 22:33

[QUOTE=3.14159;239758]Charles; Can you make scripts in Sage? I'm too lazy to go through the whole process of trying to install and run it on here.[/QUOTE]

Sage uses Python for scripting, so that's the language you would use.

science_man_88 2010-12-02 22:33

[QUOTE=CRGreathouse;239761]If you want to change the window, right-click, choose properties, choose the layout tab, and change the window size and/or the screen buffer size.[/QUOTE]

yeah well how come now when I open it it uses ? instead of what was before lol I can't find a setting there to do it lol.

CRGreathouse 2010-12-02 22:34

[QUOTE=science_man_88;239763]yeah well how come now when I open it it uses ? instead of what was before lol I can't find a setting there to do it lol.[/QUOTE]

That's the .gprc file. Go to it (probably under C:\Program Files\PARI\ or C:\Program Files (x86)\PARI\) and change the prompt line.

science_man_88 2010-12-02 22:37

[QUOTE=CRGreathouse;239764]That's the .gprc file. Go to it (probably under C:\Program Files\PARI\ or C:\Program Files (x86)\PARI\) and change the prompt line.[/QUOTE]

I stupidly recycled it lmao.

3.14159 2010-12-02 23:32

A prime number; 397^2 + 448^2, or 358313, is a prime number.

(6888*52)^2 + 1 = 128290046977 is also prime.

Lucky guesses, it seems..

CRGreathouse 2010-12-02 23:33

[QUOTE=science_man_88;239765]I stupidly recycled it lmao.[/QUOTE]

Well, now you know what it does. You can also look inside it to see what else it can do.

science_man_88 2010-12-02 23:48

[QUOTE=CRGreathouse;239773]Well, now you know what it does. You can also look inside it to see what else it can do.[/QUOTE]

now I have a way to tell if i'm an idiot oh wait this forum already has a way wonder if i can code it lol, just joking.

science_man_88 2010-12-03 01:05

1 Attachment(s)
I have started to recollect codes I tried here and I made a .gp file and got gp reading it.

but:

[CODE](21:02) gp > FaR("I hate you Alex","Alex","loser")
*** not a function: `FaR'.[/CODE]

even though(codes.gp):

[CODE]FaR(string_to_search,string_to_find,string_to_replace_with)={
my(s1=Vec(string_to_search),s2=Vec(string_to_find),s3=string_to_replace_with,good,c="",d="");
for(i=0,#s1-#s2,
good=1;
for(j=1,#s2,
if(s2[j]!=s1[j+i],
good=0;
c=concat(c,s1[i+1]);
break
)
);
if(good,
d=concat(c,s3);
print1(d);
c="";i=i+#s2
)
);
}[/CODE]

I uploaded as a text because a .gp isn't allowed lol

CRGreathouse 2010-12-03 16:17

You probably aren't reading in the right file, then. :unsure:

If you have a file like this with all the functions you normally use, I strongly recommend reading it from your .gprc file so
1. You don't need to type the command to read it in each session
2. If you make changes to the file and want to read in those changes, you can just type "\r" instead of "\r filename.gp" since it already has the proper file loaded in the 'last file used' internal string.

science_man_88 2010-12-03 16:30

1 Attachment(s)
first file

science_man_88 2010-12-03 16:31

1 Attachment(s)
second file

science_man_88 2010-12-03 16:45

I must have the read set up wrong because I can paste the function in and I'm not given (c)-> stuff to show it's entered but then i can use it but if I don't I don't get it to work.

CRGreathouse 2010-12-03 17:44

Let's try something first. Can you go to the command prompt for me? (Start, Run, then type cmd and press Enter.) Once there, type

[code]cd "C:\Program Files\PARI"
dir codes.*[/code]

Tell me what shows up.

science_man_88 2010-12-03 18:23

[QUOTE=CRGreathouse;239843]Let's try something first. Can you go to the command prompt for me? (Start, Run, then type cmd and press Enter.) Once there, type

[code]cd "C:\Program Files\PARI"
dir codes.*[/code]

Tell me what shows up.[/QUOTE]

[CODE]Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\[SPOILER]flec[/SPOILER]>cd "C:\Program Files\PARI"

C:\Program Files\PARI>dir codes.*
Volume in drive C has no label.
Volume Serial Number is [SPOILER]684A-E086[/SPOILER]

Directory of C:\Program Files\PARI

12/03/2010 01:42 PM 2,670 codes.gp
1 File(s) 2,670 bytes
0 Dir(s) 210,214,907,904 bytes free

C:\Program Files\PARI>[/CODE]

CRGreathouse 2010-12-03 18:43

Hmm, that looks fine. (From your shot of the Explorer window I thought it might have been named codes.gp.txt since Notepad likes to tack that on the names of all files.)

Try this: Open a new GP session and type ?0. What do you get?

science_man_88 2010-12-03 18:48

[QUOTE=CRGreathouse;239854]Hmm, that looks fine. (From your shot of the Explorer window I thought it might have been named codes.gp.txt since Notepad likes to tack that on the names of all files.)

Try this: Open a new GP session and type ?0. What do you get?[/QUOTE]

nothing I also can't get rid of a drive I don't know how it came to be but I can virus scan it any way to get rid of it ? I supposedly don't have the permission to do anything but virus scan.

science_man_88 2010-12-03 18:59

[QUOTE=CRGreathouse;239854]Hmm, that looks fine. (From your shot of the Explorer window I thought it might have been named codes.gp.txt since Notepad likes to tack that on the names of all files.)

Try this: Open a new GP session and type ?0. What do you get?[/QUOTE]

well i used notepad to change it so that's why it shows it. I see 3 files windows doesn't recognize lol. could that be part of it lol.

That's why [CODE][COLOR="Red"]*** [pipe:] 'C:/Program Files/PARI/perl.exe C:/Program Files/PARI/gphelp -cu 6 -fromgp '@1'' failed.[/COLOR][/CODE] pops up. gphelp isn't recognized so it can't open it.

CRGreathouse 2010-12-03 19:05

You must be typing the wrong thing because what I asked you to type doesn't use gphelp. (??gcd uses gphelp but ?0 does not.)

Here's what it look like on my Windows machine:
[code]11:13>?0

A059389 AKS Crocker
DickmanRho FaR LambertW
Li Mfactor Pascals_triangle_puzzle
Psi R RLE
bad bases bestappr2
bigdiv bigfactor binomialInterval
binomialIntervalEst bisect check
contfracback countPowerful countSquarefree
countweird dag deBruijnXi
diffset digits eps
findPeakedMax findrec findrecd
first firstbig foo
forbigprime generalizedBinomial go
gpf guess_the_number guessnumber2
hamming ierfc initial
is is1 isSPRP
isTriangular isabundant isprimepower
isprimitiveroot istwo isweird
largestSquareFactor latest lg
li list list1
lpf lucaslehmer medial
oddres pBounds palindrome
piBounds ppi prC
---- (type RETURN to continue) ----

prC2 precpow2 predicate
primeZeta primorial quad
rad repeat res3
reverse rhoest rsp
secant_root sopf star
sumdigits sumpolyk sumset
sunconj term test
testA064708 testA064708_1 testA064708_2
testrange trinv vecgcd
veclcm vecprod vecsum
ways2 ways3[/code]

By contrast, here's ??gcd:
[code]14:03>??gcd
*** [pipe:] 'C:/Program\ Files\ (x86)/PARI/perl.exe C:/Program\ Files\ (x86)
PARI/gphelp -cu 6 -fromgp 'gcd@3'' failed.[/code]

science_man_88 2010-12-03 19:06

[QUOTE=CRGreathouse;239858]You must be typing the wrong thing because what I asked you to type doesn't use gphelp. (??gcd uses gphelp but ?0 does not.)

Here's what it look like on my Windows machine:
[code]11:13>?0

A059389 AKS Crocker
DickmanRho FaR LambertW
Li Mfactor Pascals_triangle_puzzle
Psi R RLE
bad bases bestappr2
bigdiv bigfactor binomialInterval
binomialIntervalEst bisect check
contfracback countPowerful countSquarefree
countweird dag deBruijnXi
diffset digits eps
findPeakedMax findrec findrecd
first firstbig foo
forbigprime generalizedBinomial go
gpf guess_the_number guessnumber2
hamming ierfc initial
is is1 isSPRP
isTriangular isabundant isprimepower
isprimitiveroot istwo isweird
largestSquareFactor latest lg
li list list1
lpf lucaslehmer medial
oddres pBounds palindrome
piBounds ppi prC
---- (type RETURN to continue) ----

prC2 precpow2 predicate
primeZeta primorial quad
rad repeat res3
reverse rhoest rsp
secant_root sopf star
sumdigits sumpolyk sumset
sunconj term test
testA064708 testA064708_1 testA064708_2
testrange trinv vecgcd
veclcm vecprod vecsum
ways2 ways3[/code][/QUOTE]

I know I'm saying now I know why when i do the help it doesn't work lol.

CRGreathouse 2010-12-03 19:09

[QUOTE=science_man_88;239860]I know I'm saying now I know why when i do the help it doesn't work lol.[/QUOTE]

Start a new session and do it again. I suspect what you actually typed was ??0 or ?? which are entirely different (and do go through gphelp, [i]unlike[/i] ?0).

science_man_88 2010-12-03 19:11

[QUOTE=CRGreathouse;239861]Start a new session and do it again. I suspect what you actually typed was ??0 which is entirely different (and does go through gphelp, [i]unlike[/i] ?0).[/QUOTE]

[CODE](15:10) gp > ?0


(15:10) gp > ??
*** [pipe:] 'C:/Program Files/PARI/perl.exe C:/Program Files/PARI/gphelp -cu 6 -fromgp '@1'' failed.
(15:10) gp >
[/CODE]

CRGreathouse 2010-12-03 19:15

Great, so ?0 worked.

Would you post your .gprc again?

science_man_88 2010-12-03 19:16

[QUOTE=CRGreathouse;239864]Great, so ?0 worked.

Would you post your .gprc again?[/QUOTE]

[QUOTE] colors = "boldfg"
datadir = "C:/Program Files/PARI"
[COLOR="Red"]help = "C:/Program Files/PARI/perl.exe C:/Program Files/PARI/gphelp -cu 6"
prettyprinter = "C:/Program\\ Files/PARI/perl.exe C:/Program\\ Files/PARI/tex2mail -TeX -noindent -ragged -by_par"[/COLOR]
prompt = "(%H:%M) gp > " read "codes.gp"[/QUOTE]

that help ?

science_man_88 2010-12-03 19:19

[CODE]reverse(string)={
my(v=Vec(Str(string)),c="");
forstep(y=#v,1,-1,
c=concat(c,v[y])
);c
};
repeat(string,x)={
for(y=1,x,
print1(string)
);
};
guessnumber2(b)={
my(c=0,d=b,a=0);
for(x=1,b,
for(y=1,b,
if(a<c||a==c||a==d||a>d,
a=random(b),
break()
)
);
print("I guess "a" am I h,l,or e ?");
g=input();
if(g==h,
d=a,
if(g==l,
c=a,
if(g==e,
break()
)
)
)
);
};
palindrome(string) ={
my(string=eval(Vec(Str(string))));
for(x=1,ceil(#string/2),
if(string[x]!=string[#string-(x-1)],
break(),
return(string)
)
);
};
Pascals_triangle_puzzle(topvalue=151,leftsidevalue=40,bottomvalue1=11,bottomvalue2=4) = {
y=(topvalue-(4*(bottomvalue1+bottomvalue2)))/7;
x=leftsidevalue-(y+2*bottomvalue1);
z=y-x;
print(x","y","z);
};
pascals_triangle(N)= {
my(row=[],prevrow=[])
for(x=1,N,
if(x>5,break(1));
row=eval(Vec(Str(11^(x-1))));
print(row));
prevrow=row;
for(y=6,N,
for(p=2,#prevrow,
row[p]=prevrow[p-1]+prevrow[p]);
row=concat(row,1);
prevrow=row;
print(row);
);
};
RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]);
for(x=2,#string,
if(string[x]==Letter,
number+=1,
print1("("number")"Letter);
Letter=string[x];
number=1)
);print1("("number")"Letter)
};
guess_the_number(N=10)={
my(a=random(N));
print("guess the number between 0 and "N);
for(x=1,N,
if(x>1,
print("guess again")
);
b=input();
if(b==a,
break()
)
);
print("You guessed it correctly")
};
sumdigits(n) ={
if(n%9!=0,
return(n%9),
return(n%9+9)
);
};
lucaslehmer(p)={
s=Mod(4, 2^p-1);
for(x=1,p-2,
s=s^2-2
);
if(s==0,1,0);
};
FaR(string_to_search,string_to_find,string_to_replace_with)={
my(s1=Vec(string_to_search),s2=Vec(string_to_find),s3=string_to_replace_with,good,c="",d="");
for(i=0,#s1-#s2,
good=1;
for(j=1,#s2,
if(s2[j]!=s1[j+i],
good=0;
c=concat(c,s1[i+1]);
break
)
);
if(good,
d=concat(c,s3);
print1(d);
c="";i=i+#s2
)
);
};
[/CODE]

this is my code file.

CRGreathouse 2010-12-03 19:26

[QUOTE=science_man_88;239865]that help ?[/QUOTE]

Yes. Change it to
[code]colors = "boldfg"
datadir = "C:/Program Files/PARI"
prompt = "(%H:%M)"
read "codes.gp"[/code]

science_man_88 2010-12-03 19:28

[QUOTE=CRGreathouse;239868]Yes. Change it to
[code]colors = "boldfg"
datadir = "C:/Program Files/PARI"
help = "C:/Program Files/PARI/doc/users.pdf "
prompt = "(%H:%M)
read "codes.gp"[/code][/QUOTE]

[CODE]Reading GPRC: /cygdrive/c/Program Files/PARI/.gprc ...
*** expected character: '"' instead of.
...skipping line 4.
Done.

GP/PARI CALCULATOR Version 2.4.2 (development CHANGES-1.1971)
i686 running cygwin (ix86/GMP-4.2.1 kernel) 32-bit version
compiled: Dec 23 2007, gcc-3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
(readline v5.2 enabled, extended help enabled)

Copyright (C) 2000-2006 The PARI Group

PARI/GP is free software, covered by the GNU General Public License, and comes WITHOUT ANY WARRANTY WHATSOEVER.

Type ? for help, \q to quit.
Type ?12 for how to get moral (and possibly technical) support.

parisize = 4000000, primelimit = 500000
*** error opening input file: codes.gp
^--------
... skipping file 'codes.gp'
?[/CODE]

CRGreathouse 2010-12-03 19:33

Oops, missed the closed quote. I corrected my above post.

science_man_88 2010-12-03 19:38

[QUOTE=CRGreathouse;239871]Oops, missed the closed quote. I corrected my above post.[/QUOTE]

I realized that and corrected it I now get:

[CODE]Reading GPRC: /cygdrive/c/Program Files/PARI/.gprc ...Done.

GP/PARI CALCULATOR Version 2.4.2 (development CHANGES-1.1971)
i686 running cygwin (ix86/GMP-4.2.1 kernel) 32-bit version
compiled: Dec 23 2007, gcc-3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
(readline v5.2 enabled, extended help enabled)

Copyright (C) 2000-2006 The PARI Group

PARI/GP is free software, covered by the GNU General Public License, and comes WITHOUT ANY WARRANTY WHATSOEVER.

Type ? for help, \q to quit.
Type ?12 for how to get moral (and possibly technical) support.

parisize = 4000000, primelimit = 500000
*** error opening input file: codes.gp
^--------
... skipping file 'codes.gp'
(15:37)

[/CODE]

I had to get codes.gp in E for it to work I have to find a way to stop this from needing it that way sucks lol.

science_man_88 2010-12-03 19:58

it's also why the pipes don't work lol.

science_man_88 2010-12-03 20:06

[CODE](15:57)>?0

FaR Pascals_triangle_puzzle RLE guess_the_number
guessnumber2 lucaslehmer palindrome pascals_triangle
repeat reverse sumdigits[/CODE]

I forgot to submit:

[CODE]guessnumber2(b)=my(c=0,d=b,a=0);for(x=1,b,for(y=1,b,if(a<c||a==c||a==d||a>d,a=random(b),break()));print("I guess "a" am I h,l,or e ?");g=input();if(g==h,d=a,if(g==l,c=a,if(g==e,break()))))[/CODE]

CRGreathouse 2010-12-03 20:13

[QUOTE=science_man_88;239874]it's also why the pipes don't work lol.[/QUOTE]

The pipes don't work because you're on Windows. The file won't be read if it's in a different directory from where you start GP unless you give an absolute location, e.g.,

read "E:/codes.gp"
or
read "C:\Program Files\PARI\codes.gp"

I don't remember how the slashes need to be for this to work; one of /, \, or \\. Try them until one works. :smile:

science_man_88 2010-12-03 20:16

[QUOTE=CRGreathouse;239878]The pipes don't work because you're on Windows. The file won't be read if it's in a different directory from where you start GP unless you give an absolute location, e.g.,

read "E:/codes.gp"
or
read "C:\Program Files\PARI\codes.gp"

I don't remember how the slashes need to be for this to work; one of /, \, or \\. Try them until one works. :smile:[/QUOTE]

codes.gp has a copy in my PARI folder it didn't work, you gave an exact address of users.pdf that didn't work.

CRGreathouse 2010-12-03 20:17

[QUOTE=science_man_88;239876]I forgot to submit:

[CODE]guessnumber2(b)=my(c=0,d=b,a=0);for(x=1,b,for(y=1,b,if(a<c||a==c||a==d||a>d,a=random(b),break()));print("I guess "a" am I h,l,or e ?");g=input();if(g==h,d=a,if(g==l,c=a,if(g==e,break()))))[/CODE][/QUOTE]

OK, well just format it nicely (or you'll get {{lines too long}} like [url=http://rosettacode.org/mw/index.php?title=Guess_the_number/With_feedback&diff=96548&oldid=96527]here[/url]) and put it up there.

CRGreathouse 2010-12-03 20:21

[QUOTE=science_man_88;239879]codes.gp has a copy in my PARI folder it didn't work, you gave an exact address of users.pdf that didn't work.[/QUOTE]

It doesn't matter, the extended help (??) won't work no matter what I put there.

CRGreathouse 2010-12-03 20:22

You might also consider creating a user page, maybe something like
[code]{{mylangbegin}}
{{mylang|PARI/GP|intermediate}}
{{mylang|x86 assembly|beginner}}
{{mylangend}}

I submitted a solution for these tasks:
* [[Guess the number/With feedback]]
* [[Pascal's triangle]]
* [[Pascal's triangle/Puzzle]]
* [[Rate counter]]
* [[Reverse a string]]
* [[Run-length encoding]][/code]

science_man_88 2010-12-03 20:30

[CODE](16:18)>??
*** [pipe:] 'C:\Program Files\PARI\doc\users.pdf -fromgp '@1'' failed.
(16:20)>??
*** [pipe:] 'C:Program FilesPARIdocusers.pdf -fromgp '@1'' failed.
(16:21)>??
*** [pipe:] 'C:Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:22)>??
*** [pipe:] 'C:\Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:25)>??
*** [pipe:] 'C:/Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:27)>??
*** [pipe:] 'C:/Program Files\PARI\doc\users.pdf -fromgp '@1'' failed.[/CODE]

not sure which is more annoying constantly restarting PARI or all the editing lol.

science_man_88 2010-12-03 20:32

[QUOTE=CRGreathouse;239882]You might also consider creating a user page, maybe something like
[code]{{mylangbegin}}
{{mylang|PARI/GP|intermediate}}
{{mylang|x86 assembly|beginner}}
{{mylangend}}

I submitted a solution for these tasks:
* [[Guess the number/With feedback]]
* [[Pascal's triangle]]
* [[Pascal's triangle/Puzzle]]
* [[Rate counter]]
* [[Reverse a string]]
* [[Run-length encoding]][/code][/QUOTE]

God you just want me away from the forum don't you lol.

and I never completed the RLE lol.

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.

CRGreathouse 2010-12-03 21:07

[QUOTE=science_man_88;239884][CODE](16:18)>??
*** [pipe:] 'C:\Program Files\PARI\doc\users.pdf -fromgp '@1'' failed.
(16:20)>??
*** [pipe:] 'C:Program FilesPARIdocusers.pdf -fromgp '@1'' failed.
(16:21)>??
*** [pipe:] 'C:Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:22)>??
*** [pipe:] 'C:\Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:25)>??
*** [pipe:] 'C:/Program Files/PARI/doc/users.pdf -fromgp '@1'' failed.
(16:27)>??
*** [pipe:] 'C:/Program Files\PARI\doc\users.pdf -fromgp '@1'' failed.[/CODE]

not sure which is more annoying constantly restarting PARI or all the editing lol.[/QUOTE]

The restarting was only trying to track down the error; you shouldn't need to do that anymore. But do stop typing ??; it won't work for you.

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.

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.

CRGreathouse 2010-12-05 01:48

[QUOTE=science_man_88;240062]This sounds too hard, too many things to look for.[/QUOTE]

It's pretty easy in a language like Perl, where the answer (for Pari!) would be a single, short line. But Pari has almost no string-manipulation functions, so it's rather hard to do there.

science_man_88 2010-12-05 01:49

[url]http://rosettacode.org/wiki/LZW_compression[/url] is described both on wikipedia and a book I own. Doesn't sound too hard, Of course I said that about most things lol.

CRGreathouse 2010-12-05 02:10

Go for it, then.

science_man_88 2010-12-05 02:15

[QUOTE=CRGreathouse;240067]Go for it, then.[/QUOTE]

according to my book it doesn't concat anything into the library/dictionary until it hits it the second time in a string, also it talks of a simplified case of all upcase I'll have to make the starting dictionary twice as long for both cases and 52 variables to track them through 2 times i guess I must be over complicating things, doh.

CRGreathouse 2010-12-05 02:30

Maybe try something easier first, then? Perhaps

[url]http://rosettacode.org/wiki/Non-decimal_radices/Input[/url]
or
[url]http://rosettacode.org/wiki/Write_float_arrays_to_a_text_file[/url]

science_man_88 2010-12-05 13:33

[QUOTE=CRGreathouse;240069]Maybe try something easier first, then? Perhaps

[url]http://rosettacode.org/wiki/Non-decimal_radices/Input[/url][/QUOTE]

My plan for this is no less complex now.



1)Create 2, 36 index vectors then shorten them for the respective bases.
2)Read a Vec of the number to convert and use the index to add into a decimal form variable.
3)take that decimal form and use variable%b2 to get each index to return later.
4) return it.

science_man_88 2010-12-05 13:56

trouble on number 1)

[CODE][COLOR="Red"]*** syntax error, unexpected ',': ...mb1,b1,b2)=b1digs["0","1","2","3","4","5","6"
^--------------------[/COLOR][/CODE]
never mind forgot the equals sign.

3.14159 2010-12-05 15:11

Wow. I see we've collected 2000 posts.

science_man_88 2010-12-05 15:14

[QUOTE=3.14159;240117]Wow. I see we've collected 2000 posts.[/QUOTE]

I bet if we took out all of the useless ones we'd not even have half that lol.

[CODE]convert(numb1,b1,b2)= b1digs=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];b1digs=vector(b1,n,b1digs[n]);b2digs=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];b2digs=vector(b2,n,b2digs[n]);numb1=Vec(Str(numb1));a=0;c=[];forstep(y=#numb1,1,-1,for(x=1,#b1digs,if(numb1[y]==b1digs[x],a=a+(x-1)*b1^(#numb1-y))));until(floor(a/b2)==0,c=concat(b2digs[a%b2+1],c));c;[/CODE]

most useless Pari code ever lol.

CRGreathouse 2010-12-05 16:09

[QUOTE=science_man_88;240119][CODE]convert(numb1,b1,b2)= b1digs=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];b1digs=vector(b1,n,b1digs[n]);b2digs=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];b2digs=vector(b2,n,b2digs[n]);numb1=Vec(Str(numb1));a=0;c=[];forstep(y=#numb1,1,-1,for(x=1,#b1digs,if(numb1[y]==b1digs[x],a=a+(x-1)*b1^(#numb1-y))));until(floor(a/b2)==0,c=concat(b2digs[a%b2+1],c));c;[/CODE]

most useless Pari code ever lol.[/QUOTE]

You're defining the same (long) vector twice; why not avoid that?
[CODE]convert(numb1,b1,b2)={
my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],b1digs=vector(b1,n,B[n]),b2digs=vector(b2,n,B[n]),a=0,c=[]);
numb1=Vec(Str(numb1));
forstep(y=#numb1,1,-1,
for(x=1,#b1digs,
if(numb1[y]==b1digs[x],
a=a+(x-1)*b1^(#numb1-y)
)
)
);
until(floor(a/b2)==0,
c=concat(b2digs[a%b2+1],c)
);
c
};[/CODE]

It looks like you could avoid using both b1digs and b2digs by simply pulling from the general vector B each time. Usual comments on a = a + foo vs. a += foo and floor(a/foo) vs. a\foo appy.

science_man_88 2010-12-05 16:12

[QUOTE=CRGreathouse;240131]You're defining the same (long) vector twice; why not avoid that?
[CODE]convert(numb1,b1,b2)={
my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],b1digs=vector(b1,n,B[n]),b2digs=vector(b2,n,B[n]),a=0,c=[]);
numb1=Vec(Str(numb1));
forstep(y=#numb1,1,-1,
for(x=1,#b1digs,
if(numb1[y]==b1digs[x],
a=a+(x-1)*b1^(#numb1-y)
)
)
);
until(floor(a/b2)==0,
c=concat(b2digs[a%b2+1],c)
);
c
};[/CODE]

It looks like you could avoid using both b1digs and b2digs by simply pulling from the general vector B each time. Usual comments on a = a + foo vs. a += foo and floor(a/foo) vs. a\foo appy.[/QUOTE]

apply* only if you only go up to index b1, or b2 for each.

CRGreathouse 2010-12-05 16:14

[QUOTE=science_man_88;240134]apply* only if you only go up to index b1, or b2 for each.[/QUOTE]

Yes, but you can go up to b1 rather than #b1digs.

science_man_88 2010-12-05 16:22

[QUOTE=CRGreathouse;240135]Yes, but you can go up to b1 rather than #b1digs.[/QUOTE]

I got it redefined without using b1digs or b2digs, it's still a incredibly slow script lol. Can't do convert(233,4,10) in under 4 minutes in the older version.

science_man_88 2010-12-05 16:35

[CODE](12:20)>convert(101,2,4)
*** user interrupt after 12mn, 42,922 ms.[/CODE]

incredibly slow indeed. don't think my CPU dropped below 50% and I think it hit past 99% once.

okay it works well for some like:
[CODE](13:06)>convert(11111,2,34)
%150 = "v[]"
(13:06)>##
*** last result computed in 0 ms.[/CODE]

because 34 is greater than 2^5-1 it is speedy but anything <= 31 for b2 may be very slow.

I figured out why lol I forgot a statement of a=a\b2 in the until loop.

now I find it doesn't work out properly.

I figured out I changed a formula and forgot to change it back lol.

science_man_88 2010-12-05 17:26

Well I now know why because a\b2 ==0 is wrong this is always fun. It should of been a/b2 not a\b2 I got it working now if only i can get rid of the [] in the answers. I did that stuff in my original code I do that and I have a converter that should work not only to binary but between any matchups from base 2 to 36. though I should add a check of that and if they are equal spit back the string at them lol.

science_man_88 2010-12-05 21:06

[CODE]convert(numb1,b1,b2)={
my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],a=0,c="");
numb1=Vec(Str(numb1));
forstep(y=#numb1,1,-1,
for(x=0,b1,
if(numb1[y]==B[x],
a=a+(x-1)*b1^(#numb1-y)
)
)
);
until(a/b2==0,
c=concat(B[a%b2+1],c);
a=a\b2
);
c
};[/CODE]

when is it time for a new thread if it's needed lol.

this doesn't do better than binary for to base 2 conversion at least from base 36 lol.

science_man_88 2010-12-05 22:44

[QUOTE=science_man_88;240178][CODE]convert(numb1,b1,b2)={
my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],a=0,c="");
numb1=Vec(Str(numb1));
forstep(y=#numb1,1,-1,
for(x=[COLOR="Red"]1[/COLOR],b1,
if(numb1[y]==B[x],
a=a+(x-1)*b1^(#numb1-y)
)
)
);
until(a/b2==0,
c=concat(B[a%b2+1],c);
a=a\b2
);
c
};[/CODE]

when is it time for a new thread if it's needed lol.

this doesn't do better than binary for to base 2 conversion at least from base 36 lol.[/QUOTE]

another change lol

kar_bon 2010-12-06 01:52

Another solution for this task:

[code]
\\ Convert number given in base1 into base2
convert(number,base1,base2)={
my(B=Vecsmall("0123456789abcdefghijklmnopqrstuvwxyz"),a=0,c="");
number=Vecsmall(number);
forstep(y=1,#number,1,
a*=base1;
a+=number[y]-48-(number[y]>=97)*39;
);
until(a/base2==0,
c=concat(Strchr(B[a%base2+1]),c);
a=a\base2;
);
c
};
[/code]

science_man_88 2010-12-06 01:55

[QUOTE=kar_bon;240211]Another solution for this task:

[code]
\\ Convert number given in base1 into base2
convert(number,base1,base2)={
my(B=Vecsmall("0123456789abcdefghijklmnopqrstuvwxyz"),a=0,c="");
number=Vecsmall(number);
forstep(y=1,#number,1,
[COLOR="Red"] a*=base1;
a+=number[y]-48-(number[y]>=97)*39;
[/COLOR] );
until(a/base2==0,
c=concat(Strchr(B[a%base2+1]),c);
a=a\base2;
);
c
};
[/code][/QUOTE]

I just tested this and i get:

[code](21:54)>convert1(1111111,2,10)
%15 = "1111024"[/code]

I've highlighted in red what i think is throwing it off compared to the previous.

kar_bon 2010-12-06 02:25

[QUOTE=science_man_88;240212]I just tested this and i get:

[code](21:54)>convert1(1111111,2,10)
%15 = "1111024"[/code]

I've highlighted in red what i think is throwing it off compared to the previous.[/QUOTE]

Sorry, forgot to mention:

<number> has to be given as a string!

so convert1("1111111",2,10) will do the trick!

Advantage:

You can insert any 'number' in any base from 2 to 35 and convert in any of these bases.

For example:
convert1("1j3",20,10) gives "783"
convert1("783",10,16) gives "30f"

or in one step
convert1("1j3",20,16) gives "30f"

science_man_88 2010-12-06 02:35

[QUOTE=kar_bon;240213]Sorry, forgot to mention:

<number> has to be given as a string!

so convert1("1111111",2,10) will do the trick!

Advantage:

You can insert any 'number' in any base from 2 to 35 and convert in any of these bases.

For example:
convert1("1j3",20,10) gives "783"
convert1("783",10,16) gives "30f"

or in one step
convert1("1j3",20,16) gives "30f"[/QUOTE]
it can't do base 36 ? mine did.

wonder how fast each is.
tested both with the equivalent of 36^577-1 and they came up equal.

CRGreathouse 2010-12-06 04:54

[QUOTE=science_man_88;240214]it can't do base 36 ? mine did.[/QUOTE]

Sure it can, try
[code]convert("3731151013004108312", 10, 36)[/code]

kar_bon 2010-12-06 12:08

[QUOTE=science_man_88;240214]wonder how fast each is.
tested both with the equivalent of 36^577-1 and they came up equal.[/QUOTE]

I've tested both variants with (on older system):

yours: for(x=1,100000,convert(12345678901234567890,10,16)) done in 19.6 secs

mine: for(x=1,100000,convert1("12345678901234567890",10,16)) done in 10.5 secs

The main difference is:

You're using the power function and I only the multiplication:

power: a= x1*b^e1 + x2*b^e2 + x3*b^e3 + ....
multiplication: a= ((x1*b + x2)*b + x3 ...

science_man_88 2010-12-06 14:52

[QUOTE=kar_bon;240255]I've tested both variants with (on older system):

yours: for(x=1,100000,convert(12345678901234567890,10,16)) done in 19.6 secs

mine: for(x=1,100000,convert1("12345678901234567890",10,16)) done in 10.5 secs

The main difference is:

You're using the power function and I only the multiplication:

power: a= x1*b^e1 + x2*b^e2 + x3*b^e3 + ....
multiplication: a= ((x1*b + x2)*b + x3 ...[/QUOTE]

I knew you'd be better than me in like 2 seconds lol.

CRGreathouse 2010-12-06 15:26

No big deal, he knew Horner form and you didn't.

science_man_88 2010-12-06 16:11

[QUOTE=CRGreathouse;240286]No big deal, he knew Horner form and you didn't.[/QUOTE]

he should put his up, yeah I had no power this morning but it's back up now I'm kinda worried my mom supposedly had hypercapnia last night and they think codeine ( methyl-morphine) may have helped it unsurprising with morphine allergies in the family history. I know more terms than my sisters so I can describe it but I haven't gone in to see her yet. I was told to a few times I'm not sure if I have something, They think the flu shot may have triggered her so hopefully I don't do the same. they haven't given water, I think it's from the fact that unless they give a lot they acidify the blood by production of carbonic acid.

science_man_88 2010-12-06 19:41

I think I found something interesting to talk about with the lucaslehmer() axn came up with. Mind if I see what you think ?

CRGreathouse 2010-12-06 21:19

Go for it.

science_man_88 2010-12-06 21:24

[QUOTE=CRGreathouse;240370]Go for it.[/QUOTE]

if you replace 0 with s and do:

[CODE]for(x=1,100,print(lucaslehmer2(x)%x","x))[/CODE]

you get many answers printed but:

[CODE]for(x=1,100,print(lucaslehmer2(x)%11","x))[/CODE]

gets you only 2 results as far as I see.

science_man_88 2010-12-06 23:14

never mind I see a pattern forming but I bet it's well known.

kar_bon 2010-12-07 00:47

Here are some string maipulation functions, because I missed them much:

- String to upper case
- String to lower case
- Left string
- Right string
- Mid string

Functions like Find or Replace are therefore possible, too.

More suggestions:
- StrFlip: lower to upper and upper to lower in one string
- StrCap: only first character of string/word capitalize
- StrTrim: delete spaces from beginning/end of string

I think StrReverse or CheckPalindrome were done, right?

[code]
addhelp(StrToUp, "StrToUp(str): Convert a string str in upper case.");
StrToUp(str)={
my(sup=Vecsmall(str),s="");
for(n=1,#sup,
if(sup[n]>=97 && sup[n]<=122,
s=concat(s,Strchr(sup[n]=sup[n]-32)),
s=concat(s,Strchr(sup[n])))
);
s
};

addhelp(StrToLow, "StrToLow(str): Convert a string str in lower case.");
StrToLow(str)={
my(sup=Vecsmall(str),s="");
for(n=1,#sup,
if(sup[n]>=65 && sup[n]<=90,
s=concat(s,Strchr(sup[n]=sup[n]+32)),
s=concat(s,Strchr(sup[n])))
);
s
};

addhelp(StrLeft, "StrLeft(str,n): Gives back n characters from left of string str.");
StrLeft(str,n)={
my(strh=Vecsmall(str),s="");
if(n<1, return(s)); \\ n too small: return empty string
if(n>=#strh, return(str)); \\ n to big: return str
for(x=1,n,
s=concat(s,Strchr(strh[x]))
);
s
};

addhelp(StrRight, "StrRight(str,n): Gives back n characters from right of string str.");
StrRight(str,n)={
my(strh=Vecsmall(str),s="");
if(n<1, return(s)); \\ n too small: return empty
if(n>#strh, return(str)); \\ n too big: return str
for(x=#strh-n+1,#strh,
s=concat(s,Strchr(strh[x]))
);
s
};

addhelp(StrMid, "StrMid(str,n,m): Gives back m characters from position n of string str.");
StrMid(str,n,m)={
my(strh=Vecsmall(str),s="");
if(n<1, n=1); \\ n too small: set n=start of string
if(n>#strh, return(s)); \\ n too big: return empty string
if(n+m>#strh, m=#strh-n+1); \\ m too big: set m maximum length available
for(x=n,n+m-1,
s=concat(s,Strchr(strh[x]))
);
s
};
[/code]

Notes:

- The expression "sup[n]=sup[n]+32" won't work with "sup[n]+=32" !

- Typing in characters like German Umlaute are not allowed/not doable in PARI!

science_man_88 2010-12-07 01:06

[QUOTE=kar_bon;240412]
I think StrReverse or CheckPalindrome were done, right?

[/QUOTE]

So is FaR lol

CRGreathouse 2010-12-07 01:19

[QUOTE=kar_bon;240412]- The expression "sup[n]=sup[n]+32" won't work with "sup[n]+=32" ![/QUOTE]

It should...

[CODE]> v=vector(3,n,n);v[2]+=10;v
%1 = [1, 12, 3]
> v=vectorsmall(3,n,n);v[2]+=10;v
%2 = Vecsmall([1, 12, 3])[/CODE]

[QUOTE=kar_bon;240412]- Typing in characters like German Umlaute are not allowed/not doable in PARI![/QUOTE]

Right, it uses C strings rather than Unicode. A person who cared should be able to hack together a solution with enough care, but since I avoid Pari for string processing I haven't though about it much. (I have run into it, though, like when I mention French or German authors in addhelp() lines and can't use accents in their names.)

science_man_88 2010-12-07 01:25

[QUOTE=CRGreathouse;240426]It should...

[CODE]> v=vector(3,n,n);v[2]+=10;v
%1 = [1, 12, 3]
> v=vectorsmall(3,n,n);v[2]+=10;v
%2 = Vecsmall([1, 12, 3])[/CODE]



Right, it uses C strings rather than Unicode. A person who cared should be able to hack together a solution with enough care, but since I avoid Pari for string processing I haven't though about it much. (I have run into it, though, like when I mention French or German authors in addhelp() lines and can't use accents in their names.)[/QUOTE]

if I could make a list of all the alt characters you wanted in vecsmall maybe making a function alt(x) might help.

CRGreathouse 2010-12-07 01:59

[QUOTE=science_man_88;240428]if I could make a list of all the alt characters you wanted in vecsmall maybe making a function alt(x) might help.[/QUOTE]

You can't store anything but ASCII characters in Pari strings (because, underneath, they're just C strings which have the same limitation).


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

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