mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-12-02, 01:43   #1904
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
CRG approval ?
Close.

You should return 1 if the string is a palindrome and a 0 otherwise. (If you just print, you can't use the results anywhere!)

There are nitpicks (you should only do the eval/Vec/Str thing if you're starting with an integer, just use Vec otherwise, no need for my since the variable string is already local, you can round down [and this use \2] since the middle character, if any, is always equal to itself) but those don't really matter, that's just taking it to the next level.
CRGreathouse is offline   Reply With Quote
Old 2010-12-02, 01:45   #1905
3.14159
 
3.14159's Avatar
 
May 2010
Prime hunting commission.

110100100002 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Close.

You should return 1 if the string is a palindrome and a 0 otherwise. (If you just print, you can't use the results anywhere!)

There are nitpicks (you should only do the eval/Vec/Str thing if you're starting with an integer, just use Vec otherwise, no need for my since the variable string is already local, you can round down [and this use \2] since the middle character, if any, is always equal to itself) but those don't really matter, that's just taking it to the next level.
.. How about Python?

It could certainly do more tasks, IMO, although not necessarily math-related.

Something that would be nice for Python would be to be able to make a short script to do a primality proof in it. (Special-form primes, preferably, for general primes, use a PRP.)

Last fiddled with by 3.14159 on 2010-12-02 at 01:49
3.14159 is offline   Reply With Quote
Old 2010-12-02, 01:47   #1906
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Close.

You should return 1 if the string is a palindrome and a 0 otherwise. (If you just print, you can't use the results anywhere!)

There are nitpicks (you should only do the eval/Vec/Str thing if you're starting with an integer, just use Vec otherwise, no need for my since the variable string is already local, you can round down [and this use \2] since the middle character, if any, is always equal to itself) but those don't really matter, that's just taking it to the next level.

I knew the return part was coming lol. yeah I did the eval(Vec(Str())) because I don't know what type they enter so unless I do a check for each type it's easier that way. I used ceil() because my formula wouldn't work without it that way so it's either change the formula or the function I use.
science_man_88 is offline   Reply With Quote
Old 2010-12-02, 01:47   #1907
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
.. How about Python?

It could certainly do more tasks, IMO, although not necessarily math-related.
Sure, it's a general-purpose programming language. I don't use it much myself (I use C, JavaScript, Pari, SQL, Perl, and C# more often) but it's not a bad language.

I'm not sure exactly what you're asking about it.
CRGreathouse is offline   Reply With Quote
Old 2010-12-02, 01:49   #1908
3.14159
 
3.14159's Avatar
 
May 2010
Prime hunting commission.

110100100002 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Sure, it's a general-purpose programming language. I don't use it much myself (I use C, JavaScript, Pari, SQL, Perl, and C# more often) but it's not a bad language.

I'm not sure exactly what you're asking about it.
I edited a post of mine.
3.14159 is offline   Reply With Quote
Old 2010-12-02, 01:49   #1909
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I knew the return part was coming lol. yeah I did the eval(Vec(Str())) because I don't know what type they enter so unless I do a check for each type it's easier that way. I used ceil() because my formula wouldn't work without it that way so it's either change the formula or the function I use.
Frankly I would use one function for testing for palindromic integers and another for testing strings -- there's no need to have them be the same function. Actually I've never needed or written a program for palindrome testing of strings in Pari, though I have a pretty decent function ispal() for integers. If I'm working with strings I don't like to use Pari.
CRGreathouse is offline   Reply With Quote
Old 2010-12-02, 01:50   #1910
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
Something that would be nice for Python would be to be able to make a short script to do a primality proof in it. (Special-form primes, preferably, for general primes, use a PRP.)
It would be slow. Python is bad with that sort of thing, just like Pari is bad with strings.
CRGreathouse is offline   Reply With Quote
Old 2010-12-02, 01:51   #1911
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Frankly I would use one function for testing for palindromic integers and another for testing strings -- there's no need to have them be the same function. Actually I've never needed or written a program for palindrome testing of strings in Pari, though I have a pretty decent function ispal() for integers. If I'm working with strings I don't like to use Pari.
what is it with me and the list of complete things lol.

Last fiddled with by science_man_88 on 2010-12-02 at 01:55
science_man_88 is offline   Reply With Quote
Old 2010-12-02, 01:52   #1912
3.14159
 
3.14159's Avatar
 
May 2010
Prime hunting commission.

24×3×5×7 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
It would be slow. Python is bad with that sort of thing, just like Pari is bad with strings.
Bad at modular reduction?

Last fiddled with by 3.14159 on 2010-12-02 at 01:52
3.14159 is offline   Reply With Quote
Old 2010-12-02, 01:55   #1913
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
waht is it with me and the list of complete things lol.
Heh.

I have a good-sized collection of functions I use often and have modestly efficient code for. Here's an incomplete list of these functions:
Code:
addhelp(bfile, "bfile(name, v, offset=1): If v is given, creates a b-file with the values of v, using name as the A-number (given as a number or a filename). If v is not given, open the b-file name (again, as a filename or number) and return a vector of its values.");
addhelp(bigdiv, "bigdiv(a,b,c,d): Does d divide a^b - c? Same as (a^b-c)%d == 0, but faster for large b. Example: bigdiv(2,p,1,d) checks if d divides the p-th Mersenne number.");
addhelp(bigfactor, "bigfactor(a,b,c,lim,{start=2}): Find small prime factors of a^b - c (up to lim). Optional parameter start gives a starting point below which primes are not checked.");
addhelp(checkVDW, "checkVDW(vv): Given a partition vv = [p1, p2, ...] with union(p1, p2, ...) = [1, 2, ..., n], finds a lower-bound proof for van der Waerden numbers based on the partition. Returns 0 if vv is not a partition of any initial segment, and k if vv proves that W(#vv, k) > n.");
addhelp(contfracback, "contfracback(v, terms): Given a continued fraction v, gives the real number back. If terms is given, use only that many terms.");
addhelp(countPowerful, "countPowerful(lim): Number of powerful numbers up to lim. Partial sum of characteristic function of of Sloane's A001694.");
addhelp(countSquarefree, "countSquarefree(lim): Counts the number of squarefree numbers up to lim.");
addhelp(diffset, "diffset(A, B) is the set of all numbers of the form a-b, a in A, b in B.");
addhelp(digits, "digits(n): Number of decimal digits in n. Sloane's A055642.");
addhelp(dsum, "dsum(n): Digit sum of n. Sloane's A007953.");
addhelp(eps, "Returns machine epsilon for the current precision.");
addhelp(Faulhaber, "Faulhaber(e,{a='x}): Returns the polynomial for the sum 1^e + 2^e + ... + x^e, evaluated at a.");
addhelp(fibmod, "fibmod(n,m): Returns the nth Fibonacci number mod m. Same as finonacci(n)%m, but faster for large n.");
addhelp(fnice, "fnice(n): Returns a string with a 'nice' factorization of n.");
addhelp(forbigprime, "forbigprime(X=a,b,seq): the sequence is evaluated, X running over the primes between a and b. EXPERIMENTAL!");
addhelp(forodd, "forodd(X=a,b,seq): the sequence is evaluated, X running over the odds between a and b.");
addhelp(forthinprime, "forthinprime(X=a,b,seq): the sequence is evaluated, X running over the primes between a and b, even if b > primelimit. EXPERIMENTAL!");
addhelp(fortwin, "fortwin(X=a,b,seq): the sequence is evaluated, X running over the twin primes between a and b.");
addhelp(fusc, "fusc(n): Stern's diatomic series, which has many interpretations. Sloane's A002487.");
addhelp(gpf, "The greatest prime factor of a number. Sloane's A006530.");
addhelp(hamming, "hamming(n): Hamming weight of n (considered as a binary number). Sloane's A000120.");
addhelp(isFibonacci, "isFibonacci(n): Is n a Fibonacci number? Sloane's A010056; characteristic function of Sloane's A000045.");
addhelp(isHexagonal, "isHexagonal(n): Is n a hexagonal number? Characteristic function of Sloane's A000384.");
addhelp(ispow2, "ispow2(n): Is n a power of two? Characteristic function of Sloane's A000079.");
addhelp(isPowerful, "isPowerful(n): Is n powerful (min exponent 2)? Sloane's A112526; characteristic function of Sloane's A001694.");
addhelp(isprimepower, "isprimepower(n): Is n a prime power? Sloane's A010055; characteristic function of Sloane's A000961.");
addhelp(issemi, "issemi(n): Is n a semiprime? Sloane's A064911; characteristic function of Sloane's A001358.");
addhelp(isthree, "isthree(n): Is the number the sum of three squares? Sloane's A071374; characteristic function of Sloane's A000378.");
addhelp(isTriangular, "isTriangular(n): Is n a triangular number? Sloane's A010054; characteristic function of Sloane's A000217.");
addhelp(istwo, "Is the number a sum of two squares? Characteristic function of Sloane's A001481.");
addhelp(largestSquareFactor, "largestSquareFactor(n): Largest square dividing n. Sloane's A008833.");
addhelp(lg, "lg(x): Binary logarithm of x.");
addhelp(longestProgression1, "longestProgression1(v): Uses a quadratic algorithm of Jeff Erickson, which is worst-case optimal; better algorithms are available when there are long progressions (> lg #v lg lg #v).");
addhelp(longestProgression, "longestProgression(v): Finds the longest arithmetic progression in v. Assumes that v is a vector of integers sorted from smallest to largest. Uses a space-efficient naive algorithm.");
addhelp(lpf, "The least prime factor of a number. Sloane's A020639.");
addhelp(Mfactor, "Mfactor(p,lim,{start=2}): Returns factors of the Mersenne number 2^p-1 up to lim, starting at start, provided p is a prime = 3 mod 4. Same as bigfactor(2,p,1,lim,start) but faster because it checks only factors of the form 2kp+1 that are +/- 1 mod 8.");
addhelp(msb, "msb(n): Most significant bit of n: returns the greatest power of 2 <= the number. Sloane's A053644.");
addhelp(nice, "nice(o): Reformats the object o 'nicely' when possible. Currently chokes on multivariable polynomials.");
addhelp(normd, "normd(a,b): Amount of the normal distribution between a and b standard deviations. Plus/minus infinity coded as [+1]/[-1].");
addhelp(oddres, "oddres(n): Returns the greatest odd number dividing n.");
addhelp(primorial, "Returns the product of each prime less than or equal to n. Sloane's A034386.");
addhelp(prp, "prp(n,b=2): Is n a b-probable prime?");
addhelp(rad, "rad(n): Radical of n, the largest squarefree number dividing n. Sloane's A007947.");
addhelp(rnormal, "rnormal(): Returns a random normal variable with mean 0 and standard deviation 1 at the current precision.");
addhelp(rp, "rp(b): Returns a random b-bit prime.");
addhelp(sopfr, "sopfr(n): Sum of prime factors of n (with multiplicity). Sloane's A001414.");
addhelp(sopf, "sopf(n): Sum of prime factors of n. Sloane's A008472.");
addhelp(sprp, "sprp(n,b=2): Is n a b-strong probable prime?");
addhelp(sumformal, "sumformal(X=start,end,expr): Formal version of sum(X=start,end,expr). Start and end can be expressions instead of numbers.");
addhelp(sumset, "sumset(A, B) is the set of all numbers of the form a+b, a in A, b in B.");
addhelp(toC, "toC(n): Format n for use with the Pari library (e.g., with gp2c programs).");
addhelp(vecgcd, "Vector gcd: returns the gcd of all elements in the vector.");
addhelp(veclcm, "Vector lcm: returns the lcm of all elements in the vector.");
addhelp(vecprod, "vecprod(v): Product of the elements of v.");
addhelp(vecsum, "vecsum(v): Sum of the elements of v.");
addhelp(ways2, "Number of ways that n can be represented as a sum of two squares. Sloane's A000161.");
addhelp(ways3, "Number of ways that n can be represented as a sum of three squares. Sloane's A000164.");
addhelp(W, "Primary branch of Lambert's W function. Finds an L >= -1 such that L * exp(L) = x, where x >= -1/e.");
CRGreathouse is offline   Reply With Quote
Old 2010-12-02, 01:59   #1914
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
Bad at modular reduction?
Actually it's more about the addition (and, to a lesser degree, multiplciation) performance due to overhead. It's as slow (to first-order) as GP without any of the builtin functions.
CRGreathouse is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do I sometimes see all the <> formatting commands when I quote or edit? cheesehead Forum Feedback 3 2013-05-25 12:56
Passing commands to PARI on Windows James Heinrich Software 2 2012-05-13 19:19
Ubiquity commands Mini-Geek Aliquot Sequences 1 2009-09-22 19:33
64-bit Pari? CRGreathouse Software 2 2009-03-13 04:22
Are these commands correct? jasong Linux 2 2007-10-18 23:40

All times are UTC. The time now is 22:56.


Fri Aug 6 22:56:09 UTC 2021 up 14 days, 17:25, 1 user, load averages: 4.42, 4.23, 4.01

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

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.