insufficient algorithm

From: Sameh Halabi (smabeer_at_yahoo.com)
Date: 08/01/04


Date: 1 Aug 2004 07:04:20 -0700

Hello .
I'm trying to create a 12 digit number which will be as much unique as
possible . for that I created 2 algorithms , the first one (which I
bring here first ) gave a max 11 digit number with output numbers
repeating themselves of about 20 times for a million tries .
then I created another algorithm which gave me an exact 12 digit
number with numbers repeating themselves of 0 times for a million
tries , but the problem with it was that it was about 6 times slower
than the first . performance is important for me . do anybody have any
idea how to make the 2nd algorithm more sufficient or to reduce
duplicates in the 1st one :

first algorithm :
=================
#define SEQ_L 9
#define FTRCD_C_L 6
#define DATE_C_L 8

int Mpgn_gt_hash_seq (unsigned long i_soc_seq_no,
                        char * i_feature_code,
                        char * i_eff_date,
                        unsigned long *o_calculated_seq)
{
    char sKey[SEQ_L+FTRCD_C_L+DATE_C_L+1];
    char* sKeyPtr;
    double nHash=0;
    double maxNum=999999999999;

    
    memset(sKey,0,SEQ_L+FTRCD_C_L+DATE_C_L+1);

    /* Make sure i_feature_code is padded with trailing spaces
        to ensure consistency */
    for (int i=0; i<FTRCD_C_L ; i++)
    if (i_feature_code[i]<'!' || i_feature_code[i]>'z')
        i_feature_code[i]=' ';

    /* Format sKey - copy input params as strings*/
    sprintf (sKey,"%.9d%.6s%.8s",i_soc_seq_no, i_feature_code,
i_eff_date);

    /* Hash Algorithm */
    nHash=*sKey;
    sKeyPtr=sKey;
    while (*sKeyPtr++)
    {
        nHash=((unsigned long)nHash<<7) + nHash + *sKeyPtr;

       /* Ensure 12 digit value */
        if (nHash>maxNum)
        {
            nHash=nHash-(maxNum*(unsigned long)(nHash/maxNum));
        }
    }

    *o_calculated_seq=(long)nHash;
    return SUCCESS;
}

in this algorithm I had also a problem with compilation getting
warnings for line << if (nHash>maxNum) >> , that variable exceeds max
size of C .

second algorithm :
==================
#define SEQ_L 9
#define FTRCD_C_L 6
#define DATE_C_L 8

int Mpgn_gt_hash_seq (unsigned long i_soc_seq_no,
                        char * i_feature_code,
                        char * i_eff_date,
                        unsigned long *o_calculated_seq)
{
    char sKey[SEQ_L+FTRCD_C_L+DATE_C_L+1];
    char* sKeyPtr;
    double nHash=0;
    char strNum[SEQ_L+FTRCD_C_L+DATE_C_L];
    int len=0;

    memset(sKey,0,SEQ_L+FTRCD_C_L+DATE_C_L+1);

    /* Make sure i_feature_code is padded with trailing spaces
        to ensure consistency */
    for (int i=0; i<FTRCD_C_L ; i++)
    if (i_feature_code[i]<'!' || i_feature_code[i]>'z')
        i_feature_code[i]=' ';

    /* Format sKey - copy input params as strings */
    sprintf (sKey,"%.9d%.6s%.8s",i_soc_seq_no,i_feature_code,i_eff_date);

    /* Hash Algorithm */
    nHash=*sKey;
    sKeyPtr=sKey;
    sprintf(strNum,"%.0f\0",nHash);
    while (*sKeyPtr++)
    {
        nHash=((unsigned long)nHash<<7) + nHash + *sKeyPtr;
        sprintf(strNum,"%.0f\0",nHash);
        /* Ensure 12 digit value */
        len = strlen(strNum);
        if (len>12)
        {
           strncpy(strNum,&strNum[len-12-1],12);
        }
        nHash = atof(strNum);
    }

    *o_calculated_seq=(long)nHash;
    return SUCCESS;
}

as I said , this algorithm worked fine , but it was much slower than
the first .

please your assistance .

Thank you very much
Sameh .



Relevant Pages

  • Re: No Set Contains Every Computable Natural (was Church-Turing compared to Zuse-Fredkin thesis)
    ... Russell is right about Turing saying this, ... You might not want to call this an algorithm which has ... decimal representation of x digit by digit. ...
    (comp.theory)
  • Re: Powers and logic
    ... quasi wrote: ... It is easy to give an algorithm which could be calculated ... one can always find the leading digit by inspection. ... The space requirement would exceed the capacity of ...
    (sci.math)
  • Re: Large-numbers division way too slow -- what to do?
    ... > algorithm is now responsible for most of the slowness. ... I want to return the quotient or the remainder). ... In the division function, at the very bottom, I placed: ... processing the last digit becomes the remainder. ...
    (sci.crypt)
  • Why are reals uncountable yet algorithms countable (long)?
    ... If you are going to say: construct a number from one digit ... you cannot enumerate the infinite digits.I have read that, unlike reals, ... one of these algorithms would also be an algorithm ... enumeration of algorithms cannot produce all reals? ...
    (sci.math)
  • Re: Displaying large numbers
    ... The routine is meant to be used from BASIC, as it returns the address of the first ASCII digit after conversion. ... because the algorithm I've written extracts digits from least significant to most significant, the number is stored in memory that way. ... Remainder is a little number that can be held in a 8-bit register, and it's the least significant digit in decimal. ... Aseembler source code ...
    (comp.sys.sinclair)