Finding the largest prime factor on the 15c



#4

I recently had reason to learn how to program my 15c (LE), and had heard that Project Euler is a good way to familiarize oneself with a new programming language/environment. Problems 1 and 2 were trivial, but I had fun with problem 3, finding the largest prime factor of a number. Unfortunately the actual number given is two digits too long for the 15c (600851475143).

To use, key the number to test into the x register then run the program. When the program has finished the largest prime factor will be in the x register. As I'm new to programming the 15c, I'd love any advice, and be interested in more efficient solutions (both speed and size improvements) if anyone is interested.

Listing

001-42,21,11    f LBL A
002- 44 0 STO 0
003- 2 2 Try 2 and 3 first
004- 44 25 STO I
005- 32 1 GSB 1
006- 3 3
007- 44 25 STO I
008- 32 1 GSB 1
009- 5 5 Start on 5
010- 44 25 STO I

011-42,21, 0 f LBL 0 Main loop
012- 32 1 GSB 1
013- 2 2
014-44,40,25 STO + I
015- 32 1 GSB 1
016- 4 4
017-44,40,25 STO + I
018- 22 0 GTO 0

019-42,21, 1 f LBL 1 Check factor in I
020- 45 0 RCL 0
021- 11 SQRT
022- 45 25 RCL I
023-43,30, 7 g TEST 7 Factor in I > square root of what we have left
024- 22 2 GTO 2 So we're done
025- 45 0 RCL 0
026- 34 x<>y
027- 10 /
028- 42 44 f FRAC
029- 43 20 g x=0 Even division
030- 22 3 GTO 3 So do the division and check again
031- 43 32 g RTN Otherwise, on to next factor

032-42,21, 2 f LBL 2 Success, display answer
033- 45 0 RCL 0
034- 31 R/S

035-42,21, 3 f LBL 3 Store remaining number and try factor again
036- 43 36 g LSTx
037- 44 0 STO 0
038- 22 1 GTO 1


#5

A few minor suggestions. In "LBL 1", the divisibility test, you take the square root of Register 0. I'd advise taking that square root when you store Register 0, saving that in Register 1. That way, you'll not have to do myriad square roots, a relatively slow operation.

You can also speed the test slightly by using a modulo-30 or modulo-210 test (eliminates divisions by multiples of 5 and 7), just as you do now with modulo-6.

Back in the '70s we beat the dickens out of this subject to get every clock cycle out of this type of code (the HP-67 took 3 hours 45 minutes to discover that 9999999967 was prime). Fascinating that the WP 34S "isprime" returns that same result in under a second!

As I don't have and have never used an HP-15, I can't say much about how to do the divisibility test as fast as possible on same. But there are some tremendous contributors on this forum who will undoubtedly dive in.

Any takers?

#6

LBL A
STO 0
2
GSB 1
3
GSB 1
5
LBL 0
GSB 1
2
RCL + I
GSB 1
4
RCL + I
GTO 0
LBL 1
STO I
RCL 0
RCL / I
TEST 8
GTO 2
FRAC
TEST 0
RTN
RDN
LSTx
STO 0
RDN
GTO 1
LBL 2
RCL 0
R/S

My changes are:

  • Use the stack to pass parameters to subroutine.
  • Changed exit-condition: reuse value R.0 / R.I.
Otherwise very nice solution.

Kind regards

Thomas


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime Collect/Factor difference bluesun08 3 225 10-29-2013, 09:36 AM
Last Post: Eddie W. Shore
  33s, 35s & 42s--The Timex(R) Factor Matt Agajanian 7 323 09-13-2013, 12:28 AM
Last Post: Matt Agajanian
  FWIW - km to miles conversion factor on a four-banger calculator Gerson W. Barbosa 37 1,100 10-19-2011, 03:49 PM
Last Post: Martin Pinckney
  Finding extremums of function with complex derivate Lukas K. 7 310 10-17-2011, 06:06 AM
Last Post: bunuel66
  Original 15C Keyboard Test Works With 15C LE!!! DigiGal 5 323 09-26-2011, 07:33 PM
Last Post: M. Joury
  Finding prime factors on a "non-programmable" calculator Don Shepherd 6 248 09-05-2011, 10:11 AM
Last Post: Allen
  optimized prime factor finder for 12c+ Don Shepherd 2 146 09-22-2010, 12:13 PM
Last Post: Don Shepherd
  Need help finding info from ace math folks Namir 16 402 08-24-2010, 11:28 PM
Last Post: Namir
  finding Df and variation and calculing limite but ... wassims 0 81 07-25-2010, 06:57 PM
Last Post: wassims
  optimized prime factor finder for HP-32s Don Shepherd 6 261 07-07-2010, 06:23 PM
Last Post: Don Shepherd

Forum Jump: