Questions tagged [luhn]

Luhn's Algorithm is a simple checksum formula used to generate the check digit in most credit card numbers.

The Luhn algorithm is well known with credit cards but is also used in various other applications like the IMEI for cell phones.

See: https://en.wikipedia.org/wiki/Luhn_algorithm

One of the exercises in the CS50 course (tag — also CS50 Stack Exchange) is implementing Luhn's Algorithm; the tag will often show up with the tag.

207 questions
2
votes
3 answers

Implementing Luhn algorithm in JavaScript

I'm trying to port a Luhm algorithm implementation I have that's in C# to JavaScript. I got it ported over and I thought it was working, but I can't get legitimate cards to validate. My AMEX validates fine, but my two VISAs refuse to do so. Here's…
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
1
vote
3 answers

What's wrong with my Luhn algorithm code?

I'm trying to use this code to test if a sample code is a valid credit card number or not (using the Luhn algorithm) in Java. Where did I go wrong? It takes in an array of 16 one-digit numbers. Any help would be much appreciated. Thanks! private…
Hannah P
  • 89
  • 4
1
vote
1 answer

Sum up digits for Luhns algorithm in c

I'm trying to sum up all of the multiplied digits but I have no idea how to implement it. At the moment it prints 0 1 2 0 1 0 1 0 1 0 1 2 8 0 0 0 0 0 0 0 0. void get_digit(long credit_num, long n) { int sum = 0; credit_num = ((credit_num /…
imbored
  • 15
  • 1
  • 4
1
vote
1 answer

When i use my code in local vscode its giving only 63 as answer but when i use a different online complier it works fine

This is a Luhn algorithm code and it works fine in an online complier but when I use it in my local vscode it is only giving 63 as output. I dont know if its a memory issue as it late long variable. i.e credit card number as input. #include…
Karma0o7
  • 15
  • 5
1
vote
1 answer

Why is this implementation of Luhn's algorithm not working?

I was creating a function for Luhn's algorithm in Javascript. Here is my code: // All valid credit card numbers const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]; const…
Jandroid
  • 111
  • 1
  • 11
1
vote
1 answer

How to do Luhn check in df column in spark scala

df has one string column like "100256437". I want to add one more column to check whether it pass Luhn. If pass, lit(true), else lit(false) def Mod10(c: Column): Column = { var (odd, sum) = (true, 0) for (int <- c.reverse.map {…
1
vote
1 answer

Generate 16 digit number that passes Luhn algorithm

I'm trying to generate 16 digit number that passes the Luhn algorithm verification. For example, If I'm generating 16 digit number for an American Express card which would begin with 37 Something like this 3789 1477 0763 171 (this would pass the…
1
vote
3 answers

Chunk a string every odd and even position

I know nothing about javascript. Assuming the string "3005600008000", I need to find a way to multiply all the digits in the odd numbered positions by 2 and the digits in the even numbered positions by 1. This pseudo code I wrote outputs (I think)…
Chris
  • 1,667
  • 6
  • 34
  • 52
1
vote
1 answer

Valid VISA: 4222222222222 outputted as invalid

When running my program through cs50's submit50 check, everything works besides validating 4222222222222 as VISA (instead of INVALID) is output. When I print out the count variable before, then sometimes VISA is output. Any solutions would be much…
Nini
  • 23
  • 3
1
vote
1 answer

CS50 Pset1 Credit: Why isn't my program responding right?

I have basically no coding experience and am enrolled in CS50x on edX. I am currently working on Problem Set 1's Credit problem (in the C language). My program uses Luhn's Algorithm to determine whether a credit card number belongs to American…
RomVHS
  • 11
  • 3
1
vote
0 answers

Can someone tell me how to make this checksum work? Debugging has led me to function luhn_even2 to be the issue

x = 4000000000000000 y = 4000000000001000 def luhn_odd(j): j = str(j) j = list(j) sum_odd = 0 k = 0 for i in j: #For loop to get the sum of odd indices i = j[k] #iterator attached to index if k%2 != 0: …
1
vote
1 answer

I have this C code for Luhn’s Algorithm. it is running fine by testing some credit card numbers, but the code checking gives me these errors. Why?

I have this C code for Luhn’s Algorithm. it is running fine by testing some credit card numbers, but the code checking gives me these errors. Why? when running the code, all credit card numbers are checked correctly, but the code checking say this…
xin pds
  • 73
  • 6
1
vote
0 answers

I have query related to Luhn_Algorithim for credit card validation : in python

I got the solution for validating credit card number and I want to understand the logic behind the second if statement: if 2*num_list_rev[i]>9: double = double + 2*num_list_rev[i] - 9 I am new to coding so sensing this may be a way…
1
vote
1 answer

How to manipulate the decimals in an integer? CS50 Luhn's Algorithm

I want to add all the digits of an array and get the sum from them. The thing i'm struggling with is I have to multiply the numbers I get from my input by which makes an array have the possibility of two digits in one array. An example number would…
FireBirdJZ
  • 11
  • 1
1
vote
2 answers

Why is my for loop not registering the first line in its block?

I have recently started learning the basics of coding from codeacademy and was given a project involving applying something called the "Luhn Algorithm" for validating credit card numbers. I think I understand the algorithm, but I seem to be getting…