-
Notifications
You must be signed in to change notification settings - Fork 4
/
luhnfreq.py
executable file
·31 lines (26 loc) · 1 KB
/
luhnfreq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
# frequency of luhn-checksum-passing numbers between 1 & infinity
from grapher import graph
def cardLuhnChecksumIsValid(card_number):
""" checks to make sure that a number passes a luhn mod-10 credit card checksum """
card_number = ''.join(card_number.split()) # remove all whitespace
sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1
for count in range(0, num_digits):
digit = int(card_number[count])
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit - 9
sum = sum + digit
return ( (sum % 10) == 0 )
print "1/10 of (Positive Real Integers) satisfy the luhn checksum."
for number in range(1,10):
print "%s-%s=%s" % (10**(number-1),10**number,(10**number)-(10**(number-1)))
results = 0
for number in range(10**(number-1),10**number):
if cardLuhnChecksumIsValid(str(number)):
results = results+1
print ": %s" % results
print results