Busan IT/Algorithm2015. 12. 29. 12:10

Finding GCD is a good start to study algorithm due to its calculation simplicity.

 

What people commonly use for figuring out GCD is prime factorization.

 

Let's say we have two numbers, 720 and 12.

 

** image from - https://en.wikipedia.org/wiki/Greatest_common_divisor 

It is a good method for human who can figure out common divisors without inputting numbers from 1 to increasingly. However, a computer should go through that process.

 

Thus, our task is to find a good algorithm for computer to calculate GCD.

 

Let's use some 'MATH' here. =_=

 

We have numbers A, B. These two numbers are not same and bigger than 1.

 

If they have a GCD, we can write as below.

** G = GCD

A = a * G

B = b * G

 

Let's subtract them.

 

A - B = a * G - b * G

= (a - b) G

 

So, we can know that A - B also have the same GCD. We can manipulate it.

 

The GCD of A or B and A - B is same. So that we can subtract a greater number to the rest number until one of these number is zero. When one of number is 0, the rest number is GCD.

 

GCD(X, 0) = X

 

** This is because zero is always zero with any number multiplied.

ex) 0 * 1 = 0, 0 * 24098209384 = 0

 

Let's prove with practical numbers.

 

GCD(54, 18) = GCD(36, 18)

= GCD(18, 18)

= GCD(18, 0)

 

Thus, GCD(54, 18) = 18

 

 

Examine the calculation. We can notice

 

-> Subtraction goes on until a greater number becomes less than a smaller number, which is same arithmetic of "%". YEAH!

 

GCD(54, 18) = GCD(18, 54%18)

= GCD(18, 0)

 

Now, we have prepared a GCD algorithm for computer.

 

Let's make functions when inputting numbers vary from 2 to 5.

 

What we should keep in mind.

 

1. Find smallest number as divisor

2. Loop should go on until input number -1 are zero

ex) 4 numbers input for GCD, loop should go on until 3 numbers are 0

 

3. If all input numbers become 0 by divisor, divisor should be GCD.


Below is code of GCD function for 5 arguments(numbers)


int GCD5(unsigned int uiNum1, unsigned int uiNum2, unsigned int uiNum3, unsigned int uiNum4, unsigned int uiNum5)
{
  unsigned int uiCnt;  //counter for loop
  unsigned int uiDiv;  //divisor for finding GCD
  unsigned int uiNum[5];  
  unsigned int uiZero;

  //putting numbers intto array  
  uiNum[0= uiNum1;
  uiNum[1= uiNum2;
  uiNum[2= uiNum3;
  uiNum[3= uiNum4;
  uiNum[4= uiNum5;

  //finding GCD(Greatest Common Divisor)
  uiDiv = uiNum1;
  while(1)
  {
    //finding divisor
    for(uiCnt = 05 > uiCnt; ++uiCnt)
    {
      if(uiDiv > uiNum[uiCnt] && uiNum[uiCnt] != 0)
      {
        uiDiv = uiNum[uiCnt];
      }
    }
    printf("Divisor is [%d]\n", uiDiv);
    uiZero = 0;
    for(uiCnt = 05 > uiCnt; ++uiCnt)
    {
      uiNum[uiCnt] = uiNum[uiCnt] % uiDiv;
      if(0 == (uiNum[uiCnt] = uiNum[uiCnt] % uiDiv))
      {
        ++uiZero;
      }      
    }
    if(4 <= uiZero)
    {
      break;
    }    
  }
  if(uiZero == 5)
  {
    return uiDiv;
  }    
  return uiNum[0] + uiNum[1] + uiNum[2] + uiNum[3] + uiNum[4];
}






 

반응형

'Busan IT > Algorithm' 카테고리의 다른 글

Sum of serial number(without loop and if state)  (0) 2016.01.01
Posted by newind2000
Busan IT/RFID2015. 12. 9. 17:35

==================================Outline====================================

Dos Serial 통신

----------------------------------------------------------------------------

Dos Serial 통신

Non-canonical Mode를 사용하여 배열에 있는 데이터를 출력해보자.

 

 

 

**VMware를 재부팅 후 데이터를 보내니 잘되었다. 이유는 알 수 없다.

 

VMware 탓인지 리눅스 탓인지 알 수 없으나, linux 환경에서는 리더기와의 통신이 되지 않았다.

 

도스환경에서 시리얼 통신을 해보자.

 

시리얼 통신을 위해 사용해야 하는 함수는 아래와 같다.

 

1. CreatFile

2. SetupComm

3. PurgeComm

4. GetCommState

5. WriteFile

6. ReadFile

7. CloseHandle

 

 

1. CreateFile

 

HANDLE WINAPI CreateFile(

_In_ LPCTSTR lpFileName,

_In_ DWORD dwDesiredAccess,

_In_ DWORD dwShareMode,

_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,

_In_ DWORD dwCreationDisposition,

_In_ DWORD dwFlagsAndAttributes,

_In_opt_ HANDLE hTemplateFile

);

각 인자에 대한 설명 - https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

 

 

 

2. SetupComm

 

BOOL WINAPI SetupComm(

_In_ HANDLE hFile,

_In_ DWORD dwInQueue,

_In_ DWORD dwOutQueue

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa363439(v=vs.85).aspx

 

 

 

3. PurgeComm

 

BOOL WINAPI PurgeComm(

_In_ HANDLE hFile,

_In_ DWORD dwFlags

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa363428(v=vs.85).aspx

 

 

 

4. GetCommState

 

BOOL WINAPI GetCommState(

_In_ HANDLE hFile,

_Inout_ LPDCB lpDCB

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa363260(v=vs.85).aspx

 

 

5. WriteFile

 

BOOL WINAPI WriteFile(

_In_ HANDLE hFile,

_In_ LPCVOID lpBuffer,

_In_ DWORD nNumberOfBytesToWrite,

_Out_opt_ LPDWORD lpNumberOfBytesWritten,

_Inout_opt_ LPOVERLAPPED lpOverlapped

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx

 

 

 

6. ReadFile

 

BOOL WINAPI ReadFile(

_In_ HANDLE hFile,

_Out_ LPVOID lpBuffer,

_In_ DWORD nNumberOfBytesToRead,

_Out_opt_ LPDWORD lpNumberOfBytesRead,

_Inout_opt_ LPOVERLAPPED lpOverlapped

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx

 

 

7. CloseHandle

 

BOOL WINAPI CloseHandle(

_In_ HANDLE hObject

);

 

각 인자에 대한 설명 - https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms724211(v=vs.85).aspx

 

 

 

 

장비가 없음으로 내 곁에 있는 ComPortMaster와 통신하자.


RFID 리더기와 통신하면 6byte를 수신하게 된다.

 

 

CRC 값을 함수를 써서 계산하자.



 

데이터시트를 참조하여 reader기의 동작 옵션을 바꿔보자.

 

PDF p/22






 

 

반응형

'Busan IT > RFID' 카테고리의 다른 글

RFID(Radio Frequency Identification)  (0) 2015.12.08
리눅스 시리얼 통신  (0) 2015.12.07
Posted by newind2000
Busan IT/RFID2015. 12. 8. 21:35

==================================Outline====================================

RFID(Radio Frequency Identification)

----------------------------------------------------------------------------

 

RFID(Radio Frequency Identification)

 

 

RF(Radio Frequency): Wireless communication의 의미이다. 단순한 무선(wireless)의 의미와 구분되어 사용되어야 한다.

 

높은 주파수는 데이터를 빨리 보내는 것이 아니라 같은 시간에 많이 보내는 것이다.

 

RF에서 캐리어 주파수에 data 주파수를 싣는 것을 모듈레이션(modulation)이라고 한다. Data를 다시 캐리어에서 분리하는 것을 디모듈레이션(demodulation)이라고 한다.

 

주파수는 진폭(위아래 폭), 주파수(highlow의 빈도)변조가 가능하다.

 

일반 RF의 주파수는 13.56Mbs이다. RF 통신 규약 중 ISO 15693을 채택한 것을 RFID라고 한다.

 

 

칩은 전기가 있어야 작동하지만 작은 칩에 베터리를 연결하는 것은 힘들다. 소형 칩이기 때문에 안테나에 있는 전파를 전력으로 삼아 작동하게 된다. 안테나의 주파수를 인지하게 되면 사용할 데이터와 전력으로 사용할 전파를 구분하여 처리한다.

 

전자기 유도현상

 

전계와 자계의 연쇄작용으로 퍼져나가는 파장을 전자파라고 한다. 전자파는 주변에 금속이 있으면 금속으로 끌려가게 된다.

//물도 전자파를 흡수하기 때문에 비가 오는 날에는 통신에 장애가 발생할 수 있다.

 

RF는 바코드를 대체하지 못한다. 첫째는 가격, 둘째는 인식률 때문이다.

 

RFIDRF의 시리얼 통신 방식을 배우는 것이다.


 

리더기는 3가지 모드로 구분된다.

 

1. Address Mode: UIDtransponder를 구분하여 데이터를 송수신한다.

2. Non-address Mode: transponder

3. Selected Mode: UIDtransponder를 구분하여 데이터를 송수신한다. 선택된 UID와만 데이터 송수신이 가능하다.

 

 

1. Address Mode

 

2. Non-address Mode

 

 

3. Selected Mode

 





 

return 값이 0이 아니면 다 에러이다.

 

 

SET-OUTPUT: 0x71




 

//flash일 때만 유효하다.

flash의 지속시간

 

 

예제 소스코드를 non-canonical 소스에 붙혀 넣고 코딩을 해보자.

반응형

'Busan IT > RFID' 카테고리의 다른 글

Dos Serial 통신  (0) 2015.12.09
리눅스 시리얼 통신  (0) 2015.12.07
Posted by newind2000