Post Reply 
Integer part and other little tricks (CASIO fx-50F)
02-14-2017, 11:53 PM
Post: #1
Integer part and other little tricks (CASIO fx-50F)
I found a little quiz on a Hungarian forum and I found my earlier solution to CASIO fx-602P. I decided that I will solve this tiny problem on my 50F - I did not think that it will cause many trouble for me.

The quiz: It is possible to add two square number to get the number of this year?

A^2+B^2=2017 --> A,B=?

OK, this is not a big deal, let's go from B=0 to B=INT(SQRT(2017/2))=31, and calculate A=SQRT(2017-B^2) and check A==INT(A) or not. If yes, we found a solution.

How we can check a number is an integer or not? It is simple: if the fractional part of the number is greater than zero, the number is not an integer. This is OK for this calculator because on the 50F only two condition is available: x>0 and x<=M.

The problem is that, on the 50F has no FRAC() function, but we can calculate it as FRAC(A)=A-INT(A), but on the 50F has no INT() also.


First idea - FIX 0 and RND
This is the obvious solution: use the FIX function and then round the number, but when you select FIX 0, the calculator rounds the display and you can not to check the fractional part of the 'A' is positive or not:
Code:

Variables:
  K1: A (store 0 in K1 before writing the program into the calculator)
  K2: B (store 32 in K2 before writing the program into the calculator)
  K4: Year (D from Date - store 2017 in K1 before writing the program into the calculator)

LRN (MODE EXP)
------
  1
  Kin-2    // counting from 31 to 0
  Kout4
  -
  Kout2
  x^2
  =
  SQRT
  -
  RND
  Kin1
  =
  x>0?
  alpha A
  alpha B
  x>0?
------
LRN (MODE .)

FIX 0 for running
This won't works, because of rounding.

After few trying with FIX and RND I can see that this is not a solution. I need something which use only integers. Maybe the BASE-N mode? Yesss...!


Third idea (second doesn't count) - INT() with BASE-N

Code:

LRN (MODE EXP)
------
  1
  Kin-2    // counting from 31 to 0
  Kout4
  -
  Kout2
  x^2
  =
  SQRT
  -
  MODE 1    // BASE-N mode makes integer part of the number
  MODE 0    // and back to COMP mode
  Kin1      // we need to store 'A' - the algebraic stack stores the substraction three steps earlier
  =
  x>0?
  alpha A
  alpha B
  x>0?
------
LRN (MODE .)

Yes, slightly imperfect, just try it: if we has no solution this will never stops, because the program never checks the value of B>0?
Unfortunately on the 50F if a condition is true the program restarted on the first step, so we need to check B<0 and stop if this is true, but if B>=0, we need to run the program.

Fourth idea - how to make a condition with true AND false
Code:

LRN (MODE EXP)
------
  1
  Kin-2    // counting from 31 to 0
  Kout2    // preparing B<0 condition
  +        // we need to keep B=0 case for the Years which are square numbers, like Year 2025=45^2+0^2
  1
  =
  ln       // let's generate an error if B<0 and the program stops
  Kout4
  -
  Kout2
  x^2
  =
  SQRT
  -
  MODE 1    // BASE-N mode makes integer part of the number
  MODE 0    // and back to COMP mode
  Kin1      // we need to store 'A' - the algebraic stack stores the substraction three steps earlier
  =
  x>0?
  alpha A
  alpha B
  x>0?
------
LRN (MODE .)

This is works as I want and stops with an error if you have no solution or you have a solution but the Year is not a square number, or stops with 0 on the display if the Year is a square number.


Thanks for reading - I'm waiting for similar little tricks which are really useful during programming on a very limited memory.

Csaba
Find all posts by this user
Quote this message in a reply
02-15-2017, 02:52 PM (This post was last modified: 02-15-2017 04:39 PM by xerxes.)
Post: #2
RE: Integer part and other little tricks (CASIO fx-50F)
Some of the CASIOs with this type of keystroke programming have no BASE-N mode, so here a solution for the FX-180P in 17 steps:

Code:
1
Kin+2
Kout1
-
Kout2
x^2
=
SQRT
Kin3
FIX0
RND
NORM
-
Kout3
=
x^2
x>0

Input: Year in K1 and zero in K2
Output: A and B in K2 and K3
The program stops with 0, if a solution is found or gives an error, if not.

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
02-15-2017, 08:45 PM
Post: #3
RE: Integer part and other little tricks (CASIO fx-50F)
Smile OK, the two solution is not comparable, because you don't check the IP of the square root=0, you check the FP>0. This is works on the 50F also, but you can feel that FIX0+RND is works but this is a cheating Wink

BTW checking the end of the loop with only one x>0 is more elegant and you do not need to decide the max value of loop counter - but you will get all the solutions twice.

Thanks for this elegant, simplest and more portable code.
If I collect my 50F codes into a short document can I share this version with your nick?!

Have you got any other little code for these blind programmable calcs?!

Csaba
Find all posts by this user
Quote this message in a reply
02-16-2017, 09:00 AM
Post: #4
RE: Integer part and other little tricks (CASIO fx-50F)
I have most of the casios of that generation. i never understood why someone would want not a do-if-true or even skip-if-true, but a restart-if-true condition... combined with a very low number of storable steps and only two possible conditions (x>0 and x<=M), that 50f was a very quirky device.
Find all posts by this user
Quote this message in a reply
02-16-2017, 02:46 PM
Post: #5
RE: Integer part and other little tricks (CASIO fx-50F)
(02-15-2017 08:45 PM)Csaba Tizedes Wrote:  Smile OK, the two solution is not comparable, because you don't check the IP of the square root=0, you check the FP>0. This is works on the 50F also, but you can feel that FIX0+RND is works but this is a cheating Wink

The logic in the FX-180P version is, if there is a difference between X and ROUND(X), that saves one step.
The combination of FIX0, RND und NORM is very useful and it can be seen as a programming trick, because it
is not mentioned in any CASIO manual. A more obvious trick is using x^2 and SQRT, due to the missing ABS.
A size optimized program for the greatest common divisor, that makes use of it:

Code:
 01  x<->y
 02  =
 03  x^2
 04  SQRT
 05  x>0
 06  x<->y

Usage example: 861 - - 615 P1


(02-15-2017 08:45 PM)Csaba Tizedes Wrote:  Have you got any other little code for these blind programmable calcs?!

Another one is Viète's formula on the FX-180P:

Code:
 P1:

 01  Min
 02  KAC
 03  2
 04  Kin1

 P2:

 01  2
 02  Kin*1
 03  Kin+2
 04  Kout2
 05  SQRT
 06  Kin/1
 07  Kin2
 08  1
 09  M-
 10  MR
 11  x>0
 12  Kout1

Usage example: 10 P1 P2

or Fun with the Casio fx-180p / fx-3600p


(02-15-2017 08:45 PM)Csaba Tizedes Wrote:  If I collect my 50F codes into a short document can I share this version with your nick?!

Yes,of course. Nice to see that you like the programming on this very limited devices.

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
02-19-2017, 04:20 PM
Post: #6
RE: Integer part and other little tricks (CASIO fx-50F)
(02-16-2017 02:46 PM)xerxes Wrote:  A size optimized program for the greatest common divisor, that makes use of it:

Code:
 01  x<->y
 02  =
 03  x^2
 04  SQRT
 05  x>0
 06  x<->y

Usage example: 861 - - 615 P1

Hello Xerxes!

Please check your GCD routine above - is it works, are you sure?!
Pls. clarify the Usage again!

I need something similar, please read this: Inverse Fibonacci Sums (MoHPC).

Your opinion?!

Thanks!
Csaba
Find all posts by this user
Quote this message in a reply
02-19-2017, 05:27 PM
Post: #7
RE: Integer part and other little tricks (CASIO fx-50F)
Hi Csaba,

I've checked the code and it works like it should. If I type in "861 minus minus 615 P1", the output is 123.
The trick is to use a pending operator in the program loop, if possible.

I'll check the Inverse Fibonacci Sums on the FX-180P.

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
02-20-2017, 09:01 AM
Post: #8
RE: Integer part and other little tricks (CASIO fx-50F)
(02-19-2017 05:27 PM)xerxes Wrote:  "861 minus minus 615 P1", the output is 123.
Uhh..., I have tested and that was a flash in my mind, when I pressed two times the [-] minus key and the 'K - Constant calculation' is lit on the LCD.

I need to study this little program step by step...

(02-19-2017 05:27 PM)xerxes Wrote:  I'll check the Inverse Fibonacci Sums on the FX-180P.
Thanks!

Csaba
Find all posts by this user
Quote this message in a reply
02-20-2017, 01:47 PM
Post: #9
RE: Integer part and other little tricks (CASIO fx-50F)
Another way is using the K vars, instead of the pending operator:

Code:
 01  Kin-1
 02  x<->K1
 03  x^2
 04  SQRT
 05  x>0
 06  Kout1

Usage example: 861 Kin1 615 P1

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
03-16-2017, 04:11 PM (This post was last modified: 03-17-2017 01:08 PM by xerxes.)
Post: #10
RE: Integer part and other little tricks (CASIO fx-50F)
An interesting exercise from a french pocket and calculator forum is the Collatz conjecture, thats pretty simple to implement normally.
First I was not sure, if it's also possible on the very limited FX-180P, but it was. The real challenge however, was to reduce the steps
to 29 bytes or less, to fit in the FX-50F too:

Code:
 01  2
 02  Kin/1
 03  Kout1
 04  -
 05  1
 06  Kin+3
 07  =
 08  1/x
 09  Kout1
 10  Kin2
 11  FIX0
 12  RND
 13  NORM
 14  Kin-2
 15  .
 16  5
 17  Kin+2
 18  Kout2
 19  x>0
 20  6
 21  Kin*1
 22  1
 23  Kin+1
 24  Kout1
 25  x<=M
 26  Min
 27  RTN

Usage example:

KAC Min 13 Kin1 P1

program stops with error

Kout3 -> 9 (length)

MR -> 40 (maximum)

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
03-17-2017, 11:36 AM
Post: #11
RE: Integer part and other little tricks (CASIO fx-50F)
(03-16-2017 04:11 PM)xerxes Wrote:  from a french pocket and calculator forum

Thanks, can you share the link here?!

(03-16-2017 04:11 PM)xerxes Wrote:  Collatz conjecture, ... to reduce the steps to 29 bytes or less, to fit in the FX-50F:

Code:

 29  RTN

OK, I will check it - unfortunately RTN is not available on fx-50F Sad

Csaba
Find all posts by this user
Quote this message in a reply
03-17-2017, 01:33 PM
Post: #12
RE: Integer part and other little tricks (CASIO fx-50F)
Hi Csaba,

I was able to optimize a litte for speed and size with 27 bytes now. You can replace the RTN with x>0.
Can you please check the execution time on the FX-50F for 27? The FX-3900PV needs 11.3 seconds.
The link to the discussion: http://www.silicium.org/forum/viewtopic....46&t=36995

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
03-17-2017, 11:02 PM (This post was last modified: 03-18-2017 07:51 AM by Csaba Tizedes.)
Post: #13
RE: Integer part and other little tricks (CASIO fx-50F)
[edit]3400P added!

(03-17-2017 01:33 PM)xerxes Wrote:  Can you please check the execution time on the FX-50F for 27? The FX-3900PV needs 11.3 seconds.

The results:
Code:
CASIO fx-10F:   7.30s / 7.29s / 7.29s --> aver= 7.3s
CASIO fx-50F:   8.91s / 8.90s / 8.91s --> aver= 8.9s
CASIO fx-61F:   4.55s / 4.59s / 4.60s --> aver= 4.6s
CASIO fx-3400P: 7.71s / 7.71s / 7.67s --> aver= 7.7s
CASIO fx-3600P: 6.52s / 6.48s / 6.58s --> aver= 6.5s

Thanks for the link!
Csaba
Find all posts by this user
Quote this message in a reply
03-18-2017, 12:50 PM
Post: #14
RE: Integer part and other little tricks (CASIO fx-50F)
Thank you for the results. If you like this CASIO series, you will love the FX-3900Pv or the FX-180P Plus.
These are much faster and you have 300 steps in 4 areas. Additionally they offer a nice editing mode,
so it's not necessary to program in the blind any more. I assume, that your results are for 13 and not 27.
If so, the FX-3900Pv needs about 0.9 seconds.

Calculator Benchmark
Find all posts by this user
Quote this message in a reply
03-20-2017, 09:43 PM
Post: #15
RE: Integer part and other little tricks (CASIO fx-50F)
(03-18-2017 12:50 PM)xerxes Wrote:  Thank you for the results. If you like this CASIO series, you will love the FX-3900Pv or the FX-180P Plus.
These are much faster and you have 300 steps in 4 areas. Additionally they offer a nice editing mode,
so it's not necessary to program in the blind any more. I assume, that your results are for 13 and not 27.
If so, the FX-3900Pv needs about 0.9 seconds.
Yes, this was the 13 Kin1. The 27 was a little misunderstood, because the length of the program decreased to 27 steps and the example was 13.
I'll check it with 27 Kin1 also.

Csaba
Find all posts by this user
Quote this message in a reply
03-23-2017, 09:22 AM
Post: #16
RE: Integer part and other little tricks (CASIO fx-50F)
On fx-50F:
Code:
[KAC] [Min] 27 [Kin] 1 [P1]
the timing: 113.6s/113.2s/113.0s --> aver: 113.3s
[MR]: 9232, [Kout] 3: 111
The other calcs this evening.

Csaba
Find all posts by this user
Quote this message in a reply
03-06-2019, 10:22 PM
Post: #17
RE: Integer part and other little tricks (CASIO fx-50F)
Uhh, this was WTF - but I found why SD mode is required... Check in YT and read my comment below the video.





Csaba
Find all posts by this user
Quote this message in a reply
Post Reply 




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