Post Reply 
Mini-challenge: MAX(factors of 2 or 5)
01-17-2018, 04:49 AM
Post: #4
RE: Mini-challenge: MAX(factors of 2 or 5)
My implementation of Claudio's approach:

Code:
<< 0 UNROT
  WHILE DUP2 MOD NOT
  REPEAT ROT 1 + UNROT / LASTARG SWAP DROP
  END DROP SWAP
>>

'NDIVS' STO

This takes a number N in level 2 and a factor F in level 1, and returns the number M of times that F divides N in level 1, and N / F^M in level 2, and...

Code:
<< 10 NDIVS SWAP DUP 2 NDIVS
  IF DUP NOT
  THEN DROP 5 NDIVS
  END UNROT DROP2 +
>>

...takes a number in level 1 and returns the desired result in level 1.

I'm a bit conflicted about this challenge because what's mathematically elegant and what's RPL-ically elegant are probably quite different. Claudio's approach is mathematically much better than using the FACTORS function -- why find the complete prime factorization when all you want is the multiplicity of two specific factors? And what about memory usage? Also, the FACTORS approach fails if the factors you're looking at happen not to be primes.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Mini-challenge: MAX(factors of 2 or 5) - Thomas Okken - 01-17-2018 04:49 AM



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