mersenneforum.org  

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

Reply
 
Thread Tools
Old 2014-04-27, 02:06   #2443
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by vittoire View Post
hi, im new to pari/gp and i need some guidance on how to go about storing my factor value into a variable. i have for example 192384 and i need to get 1 of the prime factor of this value.

i tried storing it to a variable "a=factor(192384)" but it stores all the prime value to it. can anyone guide me how i could get 1 of the prime factor please.
where is it in the factorization matrix originally ? what column and what row ?
science_man_88 is offline   Reply With Quote
Old 2014-04-28, 02:08   #2444
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

You could do factor(192384)[1,1] to get the smallest prime factor, factor(192384)[2,1] to get the second (if it exists), and so forth.
CRGreathouse is offline   Reply With Quote
Old 2014-05-04, 00:09   #2445
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default installed a newer version of pari

I just want people to know that if some of the codes I post elsewhere don't seem correct I now have both 2.5.3 and 2.7.0 and probably another. I love some of these new add-on's to existing functions etc.
science_man_88 is offline   Reply With Quote
Old 2014-05-04, 02:19   #2446
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

yes I made another try at aligen that I would hope is at least more inclusive than previous versions:

Code:
aligentry(n)={c=[];
forpart(x=n-1,
          if(#x==#vecsort(x,,8) && numdiv(x[1]*x[#x])-2==#x && setminus(divisors(x[1]*x[#x]),Vec(x))==[1,x[1]*x[#x]],
             c=concat(c,x[1]*x[#x])
            )
          ,a=[2,n-1])
      ;c
}
science_man_88 is offline   Reply With Quote
Old 2014-05-06, 01:49   #2447
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

science_man, I don't understand what that function is trying to do. But looking back through this thread using the search term "aligen" I found this exchange, and maybe a better answer to it will help you:

Quote:
Originally Posted by science_man_88 View Post
is there an easy way to create all the distinct partitions of a number n without the number 1 involved ( though I also have a way with 1 involved I think) ? because I'm trying to speed up my aligen function and it's based on partitions.
Quote:
Originally Posted by CRGreathouse View Post
Here's some naive code:

[code]partNo1(n)={
select(v->vecmin(v)>1,partitions(n))
};
With newer versions of gp, there's now a faster way to do this:
Code:
partNo1(n)={
  partitions(n, [2, n])
};
If you wanted those partitions with only distinct parts, you can do
Code:
distinctPartNo1(n)={
  select(v->#v==#Set(v), partitions(n, [2, n]))
};
or better yet
Code:
distinctPartNo1(n)={
  my(d=sqrtint(2*n+2));
  if(d*(d+1)/2 > 2*n+2, d--);
  select(v->#v==#Set(v), partitions(n, [2, n], d))
};
which is faster and requires less memory.
CRGreathouse is offline   Reply With Quote
Old 2014-05-06, 02:03   #2448
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
science_man, I don't understand what that function is trying to do. But looking back through this thread using the search term "aligen" I found this exchange, and maybe a better answer to it will help you:





With newer versions of gp, there's now a faster way to do this:
Code:
partNo1(n)={
  partitions(n, [2, n])
};
If you wanted those partitions with only distinct parts, you can do
Code:
distinctPartNo1(n)={
  select(v->#v==#Set(v), partitions(n, [2, n]))
};
or better yet
Code:
distinctPartNo1(n)={
  my(d=sqrtint(2*n+2));
  if(d*(d+1)/2 > 2*n+2, d--);
  select(v->#v==#Set(v), partitions(n, [2, n], d))
};
which is faster and requires less memory.
then I'm confused why does forpart exists ? it goes over the partitions of it's first value, it then checks if that partition has all distinct members, then for those with the proper number of elements to be the proper divisors without 1 of it's elements at opposite ends, then and only then does it check it those are the true divisors (because that could take a long time I wanted to cut them down first). the "a=" part is the same as the end of your partitions call.
science_man_88 is offline   Reply With Quote
Old 2014-05-06, 13:15   #2449
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
then I'm confused why does forpart exists ? it goes over the partitions of it's first value, it then checks if that partition has all distinct members, then for those with the proper number of elements to be the proper divisors without 1 of it's elements at opposite ends, then and only then does it check it those are the true divisors (because that could take a long time I wanted to cut them down first). the "a=" part is the same as the end of your partitions call.
You can certainly use forpart -- and it should be easy to adapt my code to that use.
CRGreathouse is offline   Reply With Quote
Old 2014-05-06, 13:45   #2450
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
You can certainly use forpart -- and it should be easy to adapt my code to that use.
adding:

Code:
floor(solve(y=1,n-1,sum(z=1,floor(y),z)-(n-1)))
into my code cut the time in half and looks to still be as accurate to the older one. is that the type of fix you were talking about ?
science_man_88 is offline   Reply With Quote
Old 2014-05-06, 15:42   #2451
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Something like that, yes. Actually I'm a bit surprised solve() doesn't choke on that, wouldn't it be better to use something like

solve(y=1,n-1, y*(y+1)/2-n+1)\1

to remove the flat parts. I guess it doesn't really matter -- this is outside the hot path so speed is nearly irrelevant.
CRGreathouse is offline   Reply With Quote
Old 2014-05-06, 15:51   #2452
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
Something like that, yes. Actually I'm a bit surprised solve() doesn't choke on that, wouldn't it be better to use something like

solve(y=1,n-1, y*(y+1)/2-n+1)\1

to remove the flat parts. I guess it doesn't really matter -- this is outside the hot path so speed is nearly irrelevant.
yeah yours becomes faster by 19,715 ms by aligentry(130) I guess a quicker way is to check if every member of x is divisible by the primes in x. if not it can't be a list of divisors. parvector and select may be able to do this eliminating some of the checks.
science_man_88 is offline   Reply With Quote
Old 2014-05-07, 16:02   #2453
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Your original code:
Code:
aligentry0(n)={c=[];
forpart(x=n-1,
          if(#x==#vecsort(x,,8) && numdiv(x[1]*x[#x])-2==#x && setminus(divisors(x[1]*x[#x]),Vec(x))==[1,x[1]*x[#x]],
             c=concat(c,x[1]*x[#x])
            )
          ,a=[2,n-1])
      ;c
};
Cleaned up with indentation, lexical scoping (always use "my" or "local" for variables), Set, a list instead of concatenation (much faster), etc.:

Code:
aligentry1(n)={
	my(c=List());
	forpart(x=n-1,
		if(#x==#Set(x) && numdiv(x[1]*x[#x])-2==#x && setminus(divisors(x[1]*x[#x]),Vec(x))==[1,x[1]*x[#x]],
			listput(c, x[1]*x[#x])
		)
	,
		[2,n-2]
	,
		solve(y=1,n-1, y*(y+1)/2-n+1)\1
	);
	Vec(c)
};
Additional thought: you're looking for a partition x1 > ... > xn equal to the proper divisors (> 1) of x1 * xk. So a faster method would be to loop through the primes p which might be the least member, look for partitions of n-p with all parts at least q = nextprime(p+1) and at most q*(n-q)\nextprime(q+1), and then add on p 'by hand'. This trims off a lot of partitions to check, I think.
CRGreathouse 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 21:15.


Fri Aug 6 21:15:04 UTC 2021 up 14 days, 15:44, 1 user, load averages: 2.56, 2.52, 2.52

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.