Post Reply 
Programming puzzle: Longest list of regular numbers?
04-17-2017, 10:58 AM (This post was last modified: 04-17-2017 11:44 AM by e_emil.)
Post: #8
RE: Programming puzzle: Longest list of regular numbers?
Thanks for hint, Dave! Good to hear that well-known algorithms exist Smile

By the way, my nSpire CX CAS solves challenge 2 in just 15 seconds! (1543 first regular numbers, which means all of them up to 2^30). It's exactly the same algorithm as in the RPL function in one of the previous posts. I'm not quite familiar with the nSpire language and I couldn't find a command to sort a list within a program, so the function returns a partly-sorted answer and the final sorting has to be done outside of the program.

   

regular(29) returns in 15 seconds (compare it with more than 5 minutes on a 50g!!!). SortA is done in a fraction of a second.

The list returned by the function is "partly-sorted" in a sense that the order of powers of two is preserved, but the numbers between any two consecutive powers of two are not in order.

And here's the code:

Define regular(smax)=
(C) finds all regular numbers up to (but not including) 2^(smax+1)
(C) output list has to be sorted after the function returns
Func
:Local a,b,x,y,zlow,z,s,m,n,lpart,lall
:a:=((ln(2.))/(ln(3.)))
:b:=((ln(2.))/(ln(5.)))
:lpart:={}
:lall:={}
:For s,1,smax
:For x,0,s
: For y,0,floor((s+1-x)*a)
: zlow:=(s-x-((y)/(a)))*b
: z:=floor(zlow+b)
: If ceiling(zlow)=z Then
: lpart:=augment(lpart,{2^(x)*3^(y)*5^(z)})
: EndIf
: EndFor
:EndFor
(C) The list "lpart" should have to be sorted at this point but for some reason nSpire's SortA doesn't work in a function
(C) so the output list will be sorted after the function returns
:lall:=augment(lall,lpart)
:lpart:={}
:EndFor
:Return lall
:EndFunc

Update to challenge #1: the nSpire CX CAS with the above code finds 6909 first numbers (all up to 2^51) in about 90 seconds. Sorting of the output list using "SortA" is still around a second or two. "Resource exhaustion" happens shortly after this point, so to solve challenge 3, the function should not store intermediate numbers in a list.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzle: Longest list of regular numbers? - e_emil - 04-17-2017 10:58 AM



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