Jump to content
Server Maintenance This Week. ×

Custom Function Riddle


This topic is 5185 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi, I am relatively newbie with CF, and therefore need your help to make a CF which does the following (probably recursively):

From a number field (AFM) which must be 9 digits long (e.g 070878254),

1) Make a string using a Left(AFM;8) let's say for example 07087825

2)Separate each of the 8 digits and multiply it with 2 raised to a force from 8 to 1 (counting backwards), and then add all together.

That would be like:

(0*2^8)+(7*2^7)+(0*2^6)+(8*2^5)+(7*2^4)+(8*2^3)+(2*2^2)+(5*2^1)

3)Finally, it would divide the resulting number with 11, and the remainder once again divided with 10.

If the resulting number is equal to the last digit of the initial field (AFM)the function should return TRUE, otherwise FALSE.

From what I am aware of, the 3rd step requires the use of the MOD function.

I would really love to see how this can be implemented by a recursive function since I am trying to learn how to write in a recursive manner. Otherwise, I understand I could break it down to parts digit-by-digit using the MIDDLE function, multiply each separately and then add all up.

For the rest I remain by your assistance...

Edited by Guest
Link to comment
Share on other sites

I am not familiar with this algorithm. I believe steps 1 & 2 could be achieved by:

MyCustomFunction ( number )


Let ( 

len = Length ( number ) 

;

Left ( number ; 1 ) * 2^( len - 1 ) 

+

Case ( len > 2 ; MyCustomFunction ( Right ( number ; len - 1 ) ) )

)

I'm afraid I didn't understand step 3.

Link to comment
Share on other sites

My spider-senses* tell me this is checksum for something like a credit card number.

I posted a custom function to validate Australian ABN numbers** on Brian Dunnings CF web site

http://www.briandunning.com/cf/660

This uses an algorithm not unlike the one you are asking about. You may be able to adapt it to your purposes.

*It really helps to describe what you're trying to do, not just how you're trying to do it.

** Saying "Australian ABN number" is a double tautology, I know.

Link to comment
Share on other sites

To clear things out:

The afore-mentioned calculation is supposed to validate a personal Tax-Identification-Number (no idea what the australian ABN is).

Until now, the way I implement this is by capturing 8 separate digits one-by-one using the MIDDLE Function and multiplying them by 2^(Length(TIN)-n), where n=the digit's place

Therefore:

1st Digit: Middle(TIN;1;1) * 2^(9-1)+

2nd Digit: Middle (TIN;2;1) * 2^(9-2)+

3rd Digit: Middle (TIN;3;1) * 2^(9-3)+

...

8th Digit: Middle (TIN;8;1) * 2^(9-8)

As you can see, the second parameter in MIDDLE is equal to the n number used to calculate the force 2 is raised to. But also, 9 equals the Length of the TIN.

So, I am trying to implement all of this into a single function that will call itself until the n reaches 8, while adding every individual step to the previous.

After that, the total ($Total) is entered in a MOD function ( Mod(Mod($Total;11);10) )and using a CASE function, if the result equals to the last digit (9th) of the TIN, then the number is valid, otherwise not.

Can somebody help once again? For the time being, I am really awful at customizing 3rd party things, I need to learn from specific examples (sorry).

Link to comment
Share on other sites

I'll give it a go. Your step 3 doesn't make sense, I suspect what you meant was to take the remainder of dividing by 11, then the remainder of dividing that by 10 (to make the number 0-9), ie:

      Mod( Mod( x; 11 ); 10 )

That works for the one sample number (you are so generous!), here's my take, as two functions:

TinCodeCheckPos( tinCode; pos ) =


Let ( digit = GetAsNumber(Middle(tinCode; 9-pos; 1));

  Case ( pos = 9; 0;  /* stop recursing */



            pos = 0;  /* The final digit, compute the checksum and compare */

          Let( [ computed = TinCodeCheckPos( tinCode; pos+1); 

                   checkSum = Mod( Mod( computed; 11 ); 10) ];

                 checkSum = digit );



   /* Not at final digit, compute the powers for this tinCode and return it */

   TinCodeCheckPos( tinCode; pos+1) + digit * (2^pos)

  )

)





TinCodeCheck( tinCode ) =



If ( Length(tinCode)  ≠ 9; False; TinCodeCheckPos(tinCode;0) )

Link to comment
Share on other sites

This topic is 5185 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.