mersenneforum.org  

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

Reply
 
Thread Tools
Old 2010-12-14, 23:01   #2069
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by axn View Post
This is decidedly _not_ Sieve of Eratosthenes. This is called "trial division". And a slow version at that. You'll get good reference pseudocode for SoE on the web. Try to implement one of those.
Code:
sieve_of_eratosthenes(x) = d=vector((x-1),n,n+1);index=1;p=2;for(y=1,#d,if(d[y]!=0,index=y;p=d[y]);if(p^2>x,break(2));forstep(i=index+p,#d,p,if(d[i]!=0,d[i]=0)))
this better ?

The main difference to what I did here and the other is that the numbers were represented poorly in the indexes of a vector.

Last fiddled with by science_man_88 on 2010-12-14 at 23:07
science_man_88 is offline   Reply With Quote
Old 2010-12-14, 23:28   #2070
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
sieve_of_eratosthenes(x) = d=vector((x-1),n,n+1);index=1;p=2;for(y=1,#d,if(d[y]!=0,index=y;p=d[y]);if(p^2>x,break(2));forstep(i=index+p,#d,p,if(d[i]!=0,d[i]=0)))
this better ?

The main difference to what I did here and the other is that the numbers were represented poorly in the indexes of a vector.
okay this kinda takes advantage of Euler's Sieve, never mind it just has the same idea lol.

Last fiddled with by science_man_88 on 2010-12-14 at 23:43
science_man_88 is offline   Reply With Quote
Old 2010-12-14, 23:57   #2071
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

not sure how to turn it into Atkin or Sundaram
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 00:36   #2072
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I named my code to show you:
How about
Code:
primesUPTO(x)=primes(primepi(x));
?

Also: I'm proud of you for making the function return a vector instead of just printing values. You're learning!
CRGreathouse is offline   Reply With Quote
Old 2010-12-15, 01:09   #2073
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
How about
Code:
primesUPTO(x)=primes(primepi(x));
?

Also: I'm proud of you for making the function return a vector instead of just printing values. You're learning!
You're too good!!!

does your semiprime detector have anything to do with:

Code:
fa = factor(N);
    isprime( fa[,1] )
which is listed in the PARI FAQ lol.
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 01:42   #2074
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
You're too good!!!

does your semiprime detector have anything to do with:

Code:
fa = factor(N);
    isprime( fa[,1] )
which is listed in the PARI FAQ lol.
No.

Right now it looks like this:
Code:
long
issemiprime(GEN n)
{
	if (typ(n) != t_INT)
		pari_err(arither1, "issemiprime");
	if (signe(n) <= 0)
		return 0;
	
	ulong nn = itou_or_0(n);
	if (nn)
		return uissemiprime(nn);
		
	pari_sp ltop = avma;
	if (!mpodd(n)) {
		long ret = mod4(n) && isprime(shifti(n, -1));
		avma = ltop;
		return ret;
	}
	
	long p = 0;
	byteptr primepointer = diffptr;
	NEXT_PRIME_VIADIFF(p, primepointer);	// Skip 2
	for (;;)
	{
		NEXT_PRIME_VIADIFF(p, primepointer);
		if (p > 997)	// What is a good breakpoint here?
			break;
		if (dvdis(n, p))	// "1 if p divides n and 0 otherwise"
		{
			long ret = isprime(diviuexact(n, p));
			avma = ltop;
			return ret;
		}
	}
	
	if (isprime(n))
		return 0;
	
	GEN fac = Z_factor_until(n, shifti(n, -1));	// Find a nontrivial factor -- returns just the factored part
	GEN expo = gel(fac, 2);
	GEN pr = gel(fac, 1);
	long len = glength(expo);
	if (len > 2) {
		avma = ltop;
		return 0;
	}
	if (len == 2) {
		if (cmpis(gel(expo, 1), 1) > 0 || cmpis(gel(expo, 2), 1) > 0) {
			avma = ltop;
			return 0;
		}
		GEN p = gel(pr, 1);
		GEN q = gel(pr, 2);
		long ret = !cmpii(mulii(p, q), n) && isprime(p) && isprime(q);
		avma = ltop;
		return ret;
	}
	if (len == 1) {
		long e = itos(gel(expo, 1));
		if (e != 2) {
			avma = ltop;
			return 0;
		}
		GEN p = gel(pr, 1);
		long ret = !cmpii(sqri(p), n) && isprime(p);
		avma = ltop;
		return ret;
	}
	
	pari_err(talker, "Z_factor_until returned an unexpected value %Ps at n = %Ps, exiting...", fac, n);
	avma = ltop;
	return NEVER_USED;
}
but I'm not done with it. Note that this is Pari, not GP.
CRGreathouse is offline   Reply With Quote
Old 2010-12-15, 15:20   #2075
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default vector basics ?

Code:
(11:17)>v=vector(100,n,20)
%359 = [20, 20, 20, 20, 20, 20,
(11:18)>v=vector(100,n,n)
%360 = [1, 2, 3, 4, 5, 6, 7, 8,
(11:18)>v=vector(#v-1,n,v[n])
%361 = [1, 2, 3, 4, 5, 6, 7, 8,
(11:18)>v=vector(#v-1,n,v[n+1])
%362 = [2, 3, 4, 5, 6, 7, 8, 9,
Code:
(11:22)>vecsort(v)
%377 = [16, 17, 18, 19

Last fiddled with by science_man_88 on 2010-12-15 at 15:22
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 15:41   #2076
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

nice I found a faster way to reverse a vector lol, might have to update reverse with this one lol:

Code:
v=vector(#v,n,v[#v-(n-1)])
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 15:51   #2077
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
nice I found a faster way to reverse a vector lol, might have to update reverse with this one lol:

Code:
v=vector(#v,n,v[#v-(n-1)])
reversal of a Ascii string made easier:

Code:
c=Vecsmall("hello");c=vector(#c,n,c[#c-(n-1)]);print(Strchr(c))

Last fiddled with by science_man_88 on 2010-12-15 at 15:51
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 15:54   #2078
axn
 
axn's Avatar
 
Jun 2003

13DF16 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
(11:18)>v=vector(#v-1,n,v[n])
%361 = [1, 2, 3, 4, 5, 6, 7, 8,
(11:18)>v=vector(#v-1,n,v[n+1])
%362 = [2, 3, 4, 5, 6, 7, 8, 9,
vecextract might possibly be better for the these two.
axn is offline   Reply With Quote
Old 2010-12-15, 16:01   #2079
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by axn View Post
vecextract might possibly be better for the these two.
only thing I've been able to accomplish is throwing out every index I wanted to use and left with the one I wanted off.

never mind I got one half of those 2 to work but how do i do the other side ?

Code:
c=vecextract(c,vector(#c-1,n,n))
Code:
c=vecextract(c,vector(#c-1,n,n+1))
which is pretty much adding a command to what I have lol.

Last fiddled with by science_man_88 on 2010-12-15 at 16:18
science_man_88 is offline   Reply With Quote
Reply

Thread Tools


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:31.


Fri Aug 6 22:31:29 UTC 2021 up 14 days, 17 hrs, 1 user, load averages: 3.46, 3.33, 3.24

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.