Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Wednesday, January 23, 2013

Hong Kong ID Formula (HKID Number Check Digit)

此文之中文版
http://computerterminal.blogspot.hk/2005/11/hkid-calculation.html


I found out form the Google Analystic that 1/8 of the visitors to this site is to the post HKID Calculation and 1/3 of the visitors are from Russia(!!!) and USA. So i decide to write that post in English for them.

HK ID number starts from Two English Letters plus 6 digits and end with a check digit in bracket
for example: AB987654(3)

Validate the HKID by the following steps:


1) Replace the leading English alphabet with numberic value following the rules below:

Space = 36  A = 10    B = 11   C = 12   D = 13  E = 14    F = 15

G = 16    H = 17   I = 18     J = 19   K = 20    L = 21M = 22    N = 23  O = 24   P = 25   Q = 26    R = 27S = 28    T = 29   U = 30   V = 31   W = 32   X = 33Y = 34     Z = 35

2) Weighted Total 
Calculation 
   A)If double english alphabet digit,e.g. AB987654(3):
           Total = 1st Digit x9 + 2nd Digit x8 + 3rd Digit x7 + 4th Digit x6 + 5th Digit x5 + 6th Digit x4 + 7th Digit x3 +8th Digit x2

   B) Else if single english alphabet digit,e.g. E364912(5):
           E364912(5) = (Space)E364912(5)
           Total = 36 x 9 + 1st Digit x8 + 2nd Digit x7 + 3rd Digit x6 + 4th Digit x5 + 5th Digit x4 + 6th Digit x3 + 7th Digit x2 

3) CheckDigit Calculation

Remaining= Total modulo 11 

IF
a)Remaning = 0:
   CheckDigit = 0

b)Remaning = 1:
   CheckDigit = A

c)Remaning = [2-10]:
   CheckDigit = 11 - Remaining


Example:
HKID:E364912(5)
(PS:E is equal to 14 according to step 1)

324 + 14x8 + 3x7 + 6x6 + 4x5 + 9x4 + 1x3 + 2x2 = 556

556 % 11 = 68

11 - 6 = 5

So E364912(5) is a Valid HKID


Sunday, December 03, 2006

Quake 3 's InvSqrt

float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}

Math explanation here (PDF)

Monday, October 30, 2006

Here's some code for blurring an image:

float[] kernelData = new float[9];
for (int i = 0; i < kernelData.length; i++)
{
kernelData[i] = 0.15f;
}
ConvolveOp blur = new ConvolveOp(new Kernel(3, 3, kernelData));
BufferedImage blurryImage = new BufferedImage(w, h, w, h,
BufferedImage.TYPE_INT_ARGB);
blurryImage.createGraphics().drawImage(imageToBlur, blur, 0, 2);

Add time stamp to picture using Java

public static void addDate(File inputFile) throws Exception {
String inputFilename = inputFile.getPath();
System.out.println("Starting with '" + inputFilename + "'");
BufferedImage input = ImageIO.read(inputFile);
Graphics2D graphics = (Graphics2D) input.createGraphics();
Date lastModified = new Date(inputFile.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String dateStamp = sdf.format(lastModified);
int fontSize = (int) (0.4 * input.getHeight() / 10.0);
Font font = new Font("Arial", Font.BOLD, fontSize);
graphics.setFont(font);
int height = graphics.getFontMetrics().getHeight();
int width = graphics.getFontMetrics().stringWidth(dateStamp);

BufferedImage dateImage = new BufferedImage(2 * fontSize + width, 2
* fontSize + height, BufferedImage.TYPE_INT_ARGB);
Graphics2D dateGraphics = (Graphics2D) dateImage.getGraphics().create();
dateGraphics.setColor(new Color(0, 0, 0, 0));
dateGraphics.setComposite(AlphaComposite.Src);
dateGraphics.fillRect(0, 0, width, height);

dateGraphics.setComposite(AlphaComposite.SrcOver);
dateGraphics.setFont(font);
dateGraphics.setColor(Color.black);
dateGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GlyphVector gv = font.createGlyphVector(dateGraphics
.getFontRenderContext(), dateStamp);
// dateGraphics.drawString(dateStamp, fontSize, fontSize);
dateGraphics.translate(fontSize, 2 * fontSize);
for (int i = 0; i < gv.getNumGlyphs(); i++) {
Shape glyph = gv.getGlyphOutline(i);
dateGraphics.setStroke(new BasicStroke(fontSize / 10));
dateGraphics.draw(glyph);
}
float[] kernel = new float[25];
for (int i = 0; i < kernel.length; i++)
kernel[i] = 1.0f / kernel.length;
ConvolveOp cOp = new ConvolveOp(new Kernel(5, 5, kernel));
BufferedImage blurred = cOp.filter(dateImage, null);
dateGraphics.dispose();

int iWidth = input.getWidth();
int iHeight = input.getHeight();
int cHeight = iWidth * 2 / 3;
int dHeight = (iHeight - cHeight) / 2;

int y = (iHeight - dHeight) - blurred.getHeight();
int x = iWidth - blurred.getWidth();

graphics.drawImage(blurred, x - fontSize, y - 2 * fontSize, null);
graphics.setFont(font);
graphics.setColor(Color.white);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics.drawString(dateStamp, x, y);
graphics.dispose();

int lastDotIndex = inputFilename.lastIndexOf('.');
String outputFilename = inputFilename.substring(0, lastDotIndex)
+ ".new" + inputFilename.substring(lastDotIndex);
FileOutputStream out = new FileOutputStream(outputFilename);
/* encodes image as a JPEG data stream */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(input);
param.setQuality(0.9f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(input);
System.out.println("Ending with '" + inputFilename + "'");
}

Of course, it's not a pinnacle of Java2D programming, nor was it intended to. Since i don't have any imaging-related java.net project, i'm not intending to polish it to perfection. It even uses classes in the com.sun.image.codec.jpeg package. Hereby it is released to the public domain - feel free to use, reuse and abuse it in any way - but don't come back if your nuclear reactor stops functioning because of this :)

The result is quite OK:

Tuesday, January 17, 2006

Vieta's Formula

Vieta's Formula for Pi
We continue the discussion on approximating Pi by calculating the areas of a circle using inscribed and circumscribed regular polygons. We illustrate Vieta's formula, developed in 1593, the oldest exact result derived for Pi.
The Formula
Vieta's formula expresses as an infinite product of nested square roots.
Here is the implementation:public class Vieta {
public static double rhs(int n) {
double result = 0;
double rhs_1 = 0;
double rhs_2 = 0;
for (int i = 1; i <= n; ++i) {
if (i == 1) {
result = Math.sqrt(0.5 + 0.5 * Math.sqrt(0.5));
rhs_1 = result;
rhs_2 = result;
} else if (i == 2) {
result = rhs_1 * Math.sqrt(0.5 + 0.5 * rhs_1);
rhs_1 = result;
} else {
result = rhs_1 * Math.sqrt(0.5 + 0.5 * rhs_1 / rhs_2);
rhs_2 = rhs_1;
rhs_1 = result;
}
}
return result;
}
}
I also wrote a Applet for calculating PI.
http://www.myjavaserver.com/~torotime/vieta_formula.html
However,once I finished this applet, I have a new ideal that I can write it in javascript for more portable result...
applet sight~~~


Tuesday, November 01, 2005

HKID Formula (HKID Number Check Digit) 香港身份證號碼計算

Click here for the English Version of HKID Formula 



香港身份證號碼有二個英文字母,一個六位數和一個加上括號的檢驗位。

你可用以下方法計算身份證號碼以確定身份證號碼的真確性。
請把身份證號上的英文字母用數字代表,如
空格=36 A = 10 B = 11 C = 12 D = 13 E = 14 F = 15
G = 16 H = 17 I = 18 J = 19 K = 20 L = 21
M = 22 N = 23 O = 24 P = 25 Q = 26 R = 27
S = 28 T = 29 U = 30 V = 31 W = 32 X = 33
Y = 34 Z = 35

方法:
1) 計算比重積和:

    A)如果為單字母開頭,如:E364912(5):
        E364912(5) = (空格)E364912(5)
        36 x 9 + 第一位x8 + 第二位x7 + 第三位x6 + 第四位x5 + 第五位x4 + 第六位x3 + 第七位x2 = 總和

    B)如果為雙字母開頭,如:AB987654(3):
       第一位x9 + 第二位x8 + 第三位x7 + 第四位x6 + 第五位x5 + 第六位x4 + 第七位x3 +第八位x2= 總和

    C)總和除以 11 得到餘數

2)計算檢驗位:
   A)如果餘數為0,檢驗位=0

   B)如果餘數為1,檢驗位=A

   C)如果餘數為2至10 [2—10]:
      檢驗位 = 11-餘數

例子:
身份證號碼:E364912(5)
(註:E 是第一位把它換成 14)

324 + 14x8 + 3x7 + 6x6 + 4x5 + 9x4 + 1x3 + 2x2 = 556

556 / 11 得餘數 6

11 - 6 = 5

所以這身份證號碼正確。

Tuesday, June 21, 2005

Fraction Class

Today, I read some help request on writting a Fraction class, It is pretty suprise to me that the requester doesn't understand the requirement of the class, but the more ridiculous isszue that shocked me its that someone say:

"if u are in deep trouble understanding the above, you may consider pm me to get the code for some cost"

How bullshit it is !!!!

IMHO forumer should help each other with free and respect. it is meanless that these guys help only the people who pay their money. This lose the meaning of forum it lose the spirite comunity. ..

Any way here is source code of Fraction class:

public class Fraction {

final int numerator;

final int denominator;

public static final Fraction ZeroFraction = new Fraction(0, 0);

public Fraction(int numerator, int denominator)
throws IllegalArgumentException {
if (denominator == 0) {
throw new ArithmeticException("Divide by zero");
}
int simplier = Fraction.simplify(numerator, denominator);
this.numerator = numerator / simplier;
this.denominator = denominator / simplier;
}

public int getDenominator() {
return denominator;
}

public int getNumerator() {
return numerator;
}

public double toDouble() {
return numerator / denominator;
}

public String toString() {
return numerator + "/" + denominator;
}

/* a/ b + c / d = (ad + bc) / bd */
public Fraction add(Fraction f1, Fraction f2) {
int newNumerator = f1.numerator * f2.denominator + f1.denominator
* f2.numerator;
int newDenominator = (f1.denominator * f2.denominator);
return new Fraction(newNumerator, newDenominator);
}

/* a/ b - c / d = (ad - bc) / bd */
public Fraction subtract(Fraction f1, Fraction f2) {
int newNumerator = f1.numerator * f2.denominator - f1.denominator
* f2.numerator;
int newDenominator = (f1.denominator * f2.denominator);
return new Fraction(newNumerator, newDenominator);
}

/* a/ b x c / d = ac / bd */
public Fraction multiply(Fraction f1, Fraction f2) {
int newNumerator = f1.numerator * f2.numerator;
int newDenominator = f1.denominator * f2.denominator;
return new Fraction(newNumerator, newDenominator);
}

/* (a/ b )/ (c / d) = ad /bc , where c/d not equal to 0 */
public Fraction divide(Fraction f1, Fraction f2) {
if (f2.toDouble() == 0) {
throw new ArithmeticException("Divide by zero");
}
int newNumerator = f1.numerator * f2.denominator;
int newDenominator = f1.denominator * f2.numerator;
return new Fraction(newNumerator, newDenominator);
}

private static int simplify(int a, int b) {
if (a % b > 0)
return simplify(b, a % b);
else
return b;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}