Created
January 12, 2012 16:45
-
-
Save jas-/1601534 to your computer and use it in GitHub Desktop.
Luh algorithm in various languages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
bool _vCC(char *cc) | |
{ | |
int n=1; | |
int d, t=0; | |
int i=strlen(cc); | |
string d; | |
if ((cc!=0)||(strlen(cc)!=0)){ | |
for (i>0;i--){ | |
d=substr(cc, i-1, 1); | |
n=(n%2==0) ? n*2 : n; | |
n=(n>9) ? substr(n, 0, 1)+substr(n, 1, 1) : n; | |
t=t+n; | |
n++; | |
} | |
return (t%10==0) ? true : false; | |
}else{ | |
return false; | |
} | |
} | |
char *substr(const char *str, int s, int t){ | |
char *n = malloc(t+1); | |
strncpy(n, str + s, t); | |
n[t] = '\0'; | |
return n; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
bool _vCC(char *cc) | |
{ | |
int n=1; | |
int d, t=0; | |
int i=cc.length; | |
string d; | |
if ((cc!=0)||(strlen(cc)!=0)){ | |
for (i>0;i--){ | |
d=cc.substr(i-1, 1); | |
n=(n%2==0) ? n*2 : n; | |
n=(n>9) ? n.substr(0, 1)+n.substr(1, 1) : n; | |
t=t+n; | |
n++; | |
} | |
return (t%10==0) ? true : false; | |
}else{ | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _vCC(cc){ | |
if (cc){ | |
var n=1;var t=0; | |
for (var i=cc.length; i>0; i--){ | |
var d=cc.substr(i -1, 1); | |
n=(n%2==0) ? n*2 : n; | |
n=(n>9) ? n.substr(0, 1)+n.substr(1, 1) : n; | |
t=t+n; | |
n++; | |
} | |
return (t%10==0) ? true : false; | |
}else{ | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _vCC($cc) { | |
if ($cc) { | |
$c=1; | |
for ($i = strlen($cc); $i>0; $i--){ | |
$d=(int)substr($cc, $i -1, 1); | |
$d=($count%2==0) ? $d * 2 : $d; | |
$d=($d>9) ? (int)substr($d, 0, 1)+(int)substr($d, 1, 1) : $d; | |
$t=$t+$d; | |
$c++; | |
} | |
return ($t%10==0) ? true : false; | |
} else { | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -e | |
use strict; | |
sub _vCC($cc){ | |
if ($cc) { | |
my $c=1; my $t=0; | |
for (my $i=strlen($cc); $i > 0; $i--){ | |
my $d=substr($cc, $i -1, 1); | |
$d=($count%2==0) ? $d * 2 : $d; | |
$d=($d>9) ? substr($d, 0, 1)+substr($d, 1, 1) : $d; | |
$t=$t+$d; | |
$c++; | |
} | |
return ($t%10==0) ? true : false; | |
} else { | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function _vCC(){ | |
if [ -z "$1" ] ; then | |
return 0; | |
else | |
cc="$1"; | |
n=1;t=0; i=${#cc}; | |
for ($i>0; $i--) ; do | |
d=${cc:$i-1:1}; | |
$n=($n%2==0) ? $n*2 : $n; | |
$n=($n>9) ? ${n:0:1}+${n:1:1} : $n; | |
$t=$t+$n; | |
$n++; | |
done | |
return ($t%10==0) ? 1 : 0; | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment