Flawed prime factorization program and unexpected 15C simulator behaviour - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not quite HP Calculators - but related (/forum-8.html) +--- Thread: Flawed prime factorization program and unexpected 15C simulator behaviour (/thread-5154.html) Flawed prime factorization program and unexpected 15C simulator behaviour - Harald - 11-18-2015 02:54 PM This is a program I have written a couple of years ago: Code: ``` # -------------------------------------------- # HEWLETT·PACKARD 15C Simulator program # Created with version 3.3.00 # -------------------------------------------- # --------------------------------------------    000 {             }     001 {    42 21 11 } f LBL A    002 {       44  0 } STO 0    003 {           2 } 2    004 {    42 21  0 } f LBL 0    005 {       44  2 } STO 2    006 {           1 } 1    007 {       44  3 } STO 3    008 {       45  0 } RCL 0    009 {    42 21  1 } f LBL 1    010 {          11 } √x̅    011 {       44  1 } STO 1    012 {    42 21  2 } f LBL 2    013 {       45  1 } RCL 1    014 {       45  2 } RCL 2    015 {    43 30  7 } g TEST x>y    016 {       22  5 } GTO 5    017 {       45  0 } RCL 0    018 {       45  2 } RCL 2    019 {          10 } ÷    020 {       42 44 } f FRAC    021 {       43 20 } g x=0    022 {       22  4 } GTO 4    023 {           4 } 4    024 {       45  2 } RCL 2    025 {    43 30  7 } g TEST x>y    026 {       22  3 } GTO 3    027 {           2 } 2    028 {    44 20  2 } STO × 2    029 {           1 } 1    030 {    44 30  2 } STO - 2    031 {       22  2 } GTO 2    032 {    42 21  3 } f LBL 3    033 {           3 } 3    034 {    44 40  2 } STO + 2    035 {       45  3 } RCL 3    036 {          16 } CHS    037 {       44  3 } STO 3    038 {    44 40  2 } STO + 2    039 {       22  2 } GTO 2    040 {    42 21  4 } f LBL 4    041 {       45  0 } RCL 0    042 {       45  2 } RCL 2    043 {          31 } R/S    044 {          10 } ÷    045 {       44  0 } STO 0    046 {       22  1 } GTO 1    047 {    42 21  5 } f LBL 5    048 {       45  0 } RCL 0    049 {          31 } R/S    050 {           0 } 0    051 {       43 32 } g RTN # --------------------------------------------``` I calculates the prime factorization for a given number (Enter the number and execute LBL A). It works for natural numbers with a maximum of 9 digits. For larger numbers it can come up with wrong results, because the fractional part (extracted in line 20 from the divison) might get rounded off. This beeing the flaw in the program as mentioned in the topic. In such an event, the number might look divisable by a factor, where in fact it is not. For example 3333333333 returns a 2 as the first prime factor. Similarly 9876543210 returns several 2s although it only contains one. Now to the unexpected behaviour with this 15C simulator. 9876543210 returns 5 as the first prime factor. Reason being that 9876543210 Enter 2 FRAC returns 4294967296?!? What is going on here? Am I making a mistake somewhere? Or is this a bug?[/code] Cheers, Harald RE: Flawed prime factorization program and unexpected 15C simulator behaviour - Marcus von Cube - 11-18-2015 05:53 PM (11-18-2015 02:54 PM)Harald Wrote:  Reason being that 9876543210 Enter 2 / FRAC returns 4294967296?!? What is going on here? Am I making a mistake somewhere? Or is this a bug? I added a division sign before FRAC. I assume you just forgot it. Now back to your question. If it's a simulator it might just use binary arithmetic which can produce strange results. RE: Flawed prime factorization program and unexpected 15C simulator behaviour - BarryMead - 11-18-2015 07:06 PM (11-18-2015 02:54 PM)Harald Wrote:  Reason being that 9876543210 Enter 2 / FRAC returns 4294967296?!? What is going on here? Am I making a mistake somewhere? Or is this a bug? Even though the simulator uses binary floating point arithmetic native to the TCL/TK language, it usually has at least as much or more accuracy than the 10-digit BCD arithmetic used in the original HP-15C. This is a genuine bug in the simulator. After division by 2 FRAC should only return one of two values (zero, or .5) so something is corrupting the behavior of the FRAC function. I noticed that it does not happen on all 10-digit numbers either. I sent an email to Torsten Manz reporting the bug. If he responds with a fix, I will post an update in this forum. RE: Flawed prime factorization program and unexpected 15C simulator behaviour - Harald - 11-18-2015 10:34 PM (11-18-2015 05:53 PM)Marcus von Cube Wrote:   (11-18-2015 02:54 PM)Harald Wrote:  Reason being that 9876543210 Enter 2 / FRAC returns 4294967296?!? What is going on here? Am I making a mistake somewhere? Or is this a bug? I added a division sign before FRAC. I assume you just forgot it. Now back to your question. If it's a simulator it might just use binary arithmetic which can produce strange results. Thanks Marcus, I just forgot it. It is there in the program though. RE: Flawed prime factorization program and unexpected 15C simulator behaviour - Harald - 11-18-2015 10:35 PM (11-18-2015 07:06 PM)BarryMead Wrote:   (11-18-2015 02:54 PM)Harald Wrote:  Reason being that 9876543210 Enter 2 / FRAC returns 4294967296?!? What is going on here? Am I making a mistake somewhere? Or is this a bug? Even though the simulator uses binary floating point arithmetic native to the TCL/TK language, it usually has at least as much or more accuracy than the 10-digit BCD arithmetic used in the original HP-15C. This is a genuine bug in the simulator. After division by 2 FRACT should only return one of two values (zero, or .5) so something is corrupting the behavior of the FRACT function. I noticed that it does not happen on all 10-digit numbers either. I sent an email to Torsten Manz reporting the bug. If he responds with a fix, I will post an update in this forum. Thanks for confirming the bug and reporting it Barry! RE: Flawed prime factorization program and unexpected 15C simulator behaviour - Harald - 11-20-2015 09:01 AM Hi Barry, thanks for the patch, I will try it! As this thread was more about a bug in the simulator than about my program I did not include enough information about it. So: The Program is a port from the HP 25. Therefore it originally didn't use lables and R/S is used. LBL A starts the program and will return the lowest prime. Pressing R/S then will return all remaining prime factors (including duplicate ones) until 0 is displayed indicating that all prime factors have been found. Cheers, Harald RE: Flawed prime factorization program and unexpected 15C simulator behaviour - rprosperi - 11-20-2015 08:50 PM (11-19-2015 11:42 PM)BarryMead Wrote:  If you unzip this attached zip file into your HP-15C source directory and patch with the following patch commands: patch -p0