Wednesday, January 13, 2010

CODIES 1.3

###### TREAT QUESTION ######

There is a concept of some magic numbers like we have numbers from 1 to some big number and we follow some iterations on the initial sequence of numbers: 1 2 3 4 5 6 7 8......

1) In first iteration we delete the numbers leaving one gap that is we delete numbers 2 4 6 8….. Therefore now the remaining numbers are 1 3 5 7 9 11 13 15 17 19................
2) In second iteration we delete numbers leaving two gaps, hence after it left no.s are 1 3 7 9 13 15 19 21……
3) Then we delete no.s leaving 3 gaps and hence left nos. are 1 3 7 13 15 19 25…….

The numbers which are never deleted are termed as magic number.
Write a function, given a number, that whether a number is a magic no. or not.


###### TREAT QUESTION ######

2 comments:

  1. If the function returns 1, its a magic number else its not.

    int mnumb(int n)
    {
    int i,j,flag=1;
    i=2;
    for(j=3;i<=n;j++)
    {
    if(n%i==0)
    {
    flag=0;
    break;
    }
    i=i+j;
    }
    return flag;
    }

    ReplyDelete
  2. //iNumber is to be checked
    void fIsMagic (int iNumber)
    {
    int iInitialArr[SomeBigNo], iModifiedArr[SomeBigNo],iIndex,iModifiedIndex, iRemove,iLoop,iFlag,iCounter;

    /* Program executed considering SomeBigNo to be 20. */

    iFlag=0;
    iCounter=SomeBigNo;

    /* creating intial array */
    for (iIndex=0; iIndex < iCounter; iIndex++)
    {
    iIntialArr[iIndex] = iIndex+1;
    }
    iRemove=2;
    iLoop=3;

    /*iterating 3 times to get an abridged array */
    while (iLoop > 0)
    {
    iModifiedIndex=0;

    /*creating modified array */
    for(iIndex=0;iindex<iCounter;iIndex++)
    {
    if((iIndex+1) % iRemove == 0 )
    continue;
    else
    {
    iModifiedArr[iModifiedIndex] = iIntialArr[iIndex];
    iModifiedIndex++;
    }

    /* Modifing intial array for next iteration */
    for(iIndex=0;iIndex<iModifiedIndex;iIndex++)
    {
    iIntialArr[iIndex]=iModifiedArr[iIndex];
    }

    iRemove++;
    iCounter=iModifiedIndex;
    iLoop--;
    }

    /*checking the no. for magic */
    for(iIndex=0;iIndex<iModifiedIndex;iIndex++)
    {
    if (iModifiedArr[iIndex] == iNumber)
    iFlag=1;
    }
    if (1== iFlag)
    {
    printf ("\n is a magic no.");
    }
    else
    {
    printf ("\n not a magic no.");
    }
    }

    ReplyDelete