Post Reply 
HHC 2015 RPL Programming Contest
10-20-2015, 02:18 AM
Post: #6
RE: HHC 2015 RPL Programming Contest
I worry that your solution won't work if the list contains integers that exceed to precision of real numbers.

I thought of a way to do it without sorting: the list is distinct primes if the least common multiple of the list is the same as the product of the list:
Code:
« DUP œLIST  SWAP  @ Put product of list in lvl 2
  1 +                   @ ugh. So STREAM works if list size == 1
  « LCM » STREAM           @ Get LCM of list
  ==
»

But this isn't quite right. If the list contains one or more copies of 1, it will incorrectly report that it is distinct primes. My next attempt was faster so I abandoned this.

Here's a version that tries to take advantage of the large size of the list. It exits as soon as it finds a duplicate or a non-prime. This takes advantage of DOLIST and error processing for speed, and to get a consistent stack when it's done.
Code:
«
  IFERR
  1 SWAP SORT
  1.
  «
    @ L2 = last number. L1 = current number
    IF DUP UNROT ==  @ duplicate?
       OVER ISPRIME? NOT OR THEN 1. DOERR END
  »
  DOLIST
  THEN 0.  @ Error thrown
  ELSE 1.
  END
  NIP
»
I generated a list of the first 250 primes larger than 8000. If finishes this list in 20.65 seconds (on the emulator set at authentic speed). But if I add 88 to the list, it finishes in just 12.13 seconds.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2015 RPL Programming Contest - David Hayden - 10-20-2015 02:18 AM



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