HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares

04132018, 11:35 AM
(This post was last modified: 04132018 11:35 AM by Gerald H.)
Post: #1




HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
As is wellknown, every integer can be represented as sum of max 4 squares
https://en.wikipedia.org/wiki/Lagrange%2...re_theorem & that the number of such representations can be calculated as https://en.wikipedia.org/wiki/Jacobi%27s...re_theorem respecting order & sign. The task is to write a User RPL programme to calculate the number of representations of any natural number exactly & swiftly. Winner will be programme with lowest value of time*cuberoot(size). I have a programme that processes 720^20 in 5.6 sec. 

04132018, 01:12 PM
(This post was last modified: 04132018 01:12 PM by pier4r.)
Post: #2




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Gerald thanks for the programming challenge.
What I find interesting is the metric. (04132018 11:35 AM)Gerald H Wrote: Winner will be programme with lowest value of time*cuberoot(size). My perspective is that size is often paramount (likely due to the limited memory of older models). Weighting the speed with the size is also done, but with the size being as important as the speed well. Applying a sort of a filter to the size, the cube root, so to make two programs that have similar size not too different (well aside from the speed factor) is pretty neat. I'd like to add maintainability as well, but that is hard to define. One should set like a commitee for it. Wikis are great, Contribute :) 

04142018, 05:10 PM
(This post was last modified: 04142018 05:39 PM by Thomas Ritschel.)
Post: #3




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
I have a solution based on Joe Horn's \Gs (sum of divisors) routine:
Code:
Code: %%HP: T(3)A(R)F(.); For 720^20 it computed the result 42050501860687733694307540234728644161563553655537254442648 in less than 2 seconds (Edit: actually 0.8 sec). However, it has a flaw: It doesn't work for the inputs 1 and 4 (\Gs fails if n==1). Open for improvements... 

04142018, 06:17 PM
Post: #4




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
I have copied your programmes, Thomas, & for input
720^20 the programme returned 52175039830928864354013492999359544 in 1.42 sec. 

04142018, 08:18 PM
(This post was last modified: 04142018 08:36 PM by Thomas Ritschel.)
Post: #5




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
(04142018 06:17 PM)Gerald H Wrote: I have copied your programmes, Thomas, & for input In my test I had 720^20 as an algebraic object on stack level 1, e.g. entered as '720^20'. However, when 720^20 is evaluated to 1401683395356260729391818575873415577600000000000000000000 first, and then the Jacobi program is called, I also get 52175039830928864354013492999359544 in about 1.4 sec. It turns out, that it fails for some kind of algebraic terms like 'a^b' or 'a*b', but not all of them (e.g. '3*11' seems to work well). To make it save, an 'EVAL' should be added to the Jacobi program: Code: %%HP: T(3)A(R)F(.); 

04152018, 12:08 AM
Post: #6




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
.
Hi, Gerald H: (04132018 11:35 AM)Gerald H Wrote: The task is to write a User RPL programme to calculate the number of representations of any natural number exactly & swiftly. Nice challenge, regrettably I can't take part in it because it asks for an RPL solution, which I can't provide. However, you say that your program processes 720^20 in 5.6 sec and that's *amazing* speed indeed for such a big number, so I'd like to ask the following: How long does your program take to process the vey slightly smaller number 720^203 ? And how long for the even smaller 720^2018 ? Thanks in advance and have a nice weekend. V. . 

04152018, 04:58 AM
Post: #7




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Yes, Valentin,
5.6 sec is amazing for 720^20 , but the value was craftily chosen to only contain small factors each to a high power, as part of the algorithm my programme implements requires factorization of the input. Similarly, 720^203 is craftily chosen to have large factors, thus practically rendering the calculation impossible on the 50g using my algorithm. However, a more crafty algorithm obviating the need to factorize could enable the calculation on the 50g & I'm hopeful one may be found by a Forum member. 

04152018, 08:36 AM
Post: #8




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Nice observations Valentin and Gerald! (And Thomas of course).
I for one, when a challenge is about the "context X" , wouldn't mind to see solutions "out of the box". Can someone get 720^203 factorized at all (in a calculator context), and only after that factorized quickly? (Same for 720^2018 and all the others hard variants) Wikis are great, Contribute :) 

04152018, 11:42 AM
(This post was last modified: 04152018 11:44 AM by Thomas Ritschel.)
Post: #9




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Here is an improved version of my earlier program.
It is still using the idea from Joe's \Gs routine, but omits the even divisors: Code: %%HP: T(3)A(R)F(.); Now it properly treats trivial inputs like 1 or 4 and works for algebraic inputs (like '720^20') as well. The runtime has also improved: 0.66 sec for 720^20 entered as 1401683395356260729391818575873415577600000000000000000000 0.69 sec for 720^20 entered as '720^20' 

04152018, 02:07 PM
Post: #10




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
I've got it further improved and significantly smaller:
Code: %%HP: T(3)A(R)F(.); Runtime for 720^20 and '720^20' is slightly slower: 0.69 and 0.72 secs, respectively. 

04152018, 03:09 PM
Post: #11




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Very good, Thomas, for your latest version I have 720^20 returning correct value in .78 sec.
Programme returns wrong values for input 0 & 1. 

04152018, 03:34 PM
(This post was last modified: 04152018 03:35 PM by Thomas Ritschel.)
Post: #12




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
(04152018 03:09 PM)Gerald H Wrote: Programme returns wrong values for input 0 & 1. For input 1 it returns 8, which should be correct (= 2*4 possibilities). But you're right, for 0 it should return 1 (not 8). The following version of the program also treats 0 correctly: Code: %%HP: T(3)A(R)F(.); 

04152018, 04:38 PM
Post: #13




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Below my programme.
For 107 random 12 digit integers the ratio of My Prog Time : Your Prog Time is .84. Your programme returns ? for input 1. Size: 190. CKSUM: # 4015d Code: « DUP 

04152018, 06:54 PM
(This post was last modified: 04152018 06:55 PM by Thomas Ritschel.)
Post: #14




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
(04152018 04:38 PM)Gerald H Wrote: Your programme returns Well, by "any natural number" (quoting your initial post) I assumed positive integers (e.g. without a decimal dot). By putting a R\>I in front of my program, it also works for numbers like 1. or 2. Code: %%HP: T(3)A(R)F(.); 

04152018, 07:06 PM
Post: #15




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
Sorry, Thomas, my text was not clear  for input
1 the programme returns ? 

04152018, 07:11 PM
Post: #16




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares  
04162018, 01:19 PM
Post: #17




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
By dropping the ability to treat algebraic inputs and some minor changes I got my version slightly faster, but it still doesn't reach the speed of your program, Gerald, at the smaller inputs.
Code: %%HP: T(3)A(R)F(.); 

04162018, 11:31 PM
Post: #18




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
(04152018 08:36 AM)pier4r Wrote: Nice observations Valentin and Gerald! (And Thomas of course). Thanks for your appreciation and kind comment. Quote:Can someone get 720^203 factorized at all (in a calculator context), and only after that factorized quickly? (Same for 720^2018 and all the others hard variants) I very much doubt it's doable on any 2018era calculator (let alone "quickly") as 720^203 has two 23digit prime factors and 720^2018 has a 22digit prime factor and a 35digit one. In similar fashion, 720^2019 and 720^20+19 are both prime. Regards. V. . 

04172018, 12:10 AM
Post: #19




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
(04162018 11:31 PM)Valentin Albillo Wrote:(04152018 08:36 AM)pier4r Wrote: Nice observations Valentin and Gerald! (And Thomas of course). No surprises here just tried factorising 720^203 on an HP Prime and it throws a quadratic sieve failure 

04172018, 05:03 AM
Post: #20




RE: HP 50g Programming Competition: How Many Partitions of an Integer in 4 Squares
On a Samsung S4 Android mobile phone
720^203 is completely factorized in seconds using factorint function in PariDroid https://play.google.com/store/apps/detai....paridroid 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)