I need a regex to match a value in which each character can be a digit from 0 to 9 or a space. The value must contain exactly 11 digits.
For example, it should match values in the format '012 345 678 90' or '01234567890'.
Can anyone please help me on this?
For the sake of other Aussie developers out there who may find this in the future:
^(\d *?){11}$
Matches 11 digits with zero or more spaces after each digit.
Edit:
As #ElliottFrisch mentioned, ABNs also have a mathematical formula for proper validation. It would be very difficult (or impossible) to use regex to properly validate an ABN - although the above regex would at least match 11 digit numbers with spacing. If you're after actual validation, perhaps regex isn't the tool for you in this case.
Further reading:
https://abr.business.gov.au/Help/AbnFormat
Here's a PHP implementation:
http://www.clearwater.com.au/code
Code copied from the above page in case it becomes unavailable someday:
// ValidateABN
// Checks ABN for validity using the published
// ABN checksum algorithm.
//
// Returns: true if the ABN is valid, false otherwise.
// Source: http://www.clearwater.com.au/code
// Author: Guy Carpenter
// License: The author claims no rights to this code.
// Use it as you wish.
function ValidateABN($abn)
{
$weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
// strip anything other than digits
$abn = preg_replace("/[^\d]/","",$abn);
// check length is 11 digits
if (strlen($abn)==11) {
// apply ato check method
$sum = 0;
foreach ($weights as $position=>$weight) {
$digit = $abn[$position] - ($position ? 0 : 1);
$sum += $weight * $digit;
}
return ($sum % 89)==0;
}
return false;
}
And a javascript one which I found here:
http://www.mathgen.ch/codes/abn.html
REF OLD DEAD LINK: http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm&pc=001/003/021/002/001&mnu=610&mfp=001/003&st=&cy=1
Confirm an ABN
The following formula can be used to verify the ABN you are allocated or to verify the ABN issued to a business you deal with.
To verify an ABN:
Subtract 1 from the first (left) digit to give a new eleven digit number
Multiply each of the digits in this new number by its weighting factor
Sum the resulting 11 products.
Divide the total by 89, noting the remainder.
If the remainder is zero the number is valid.
Digit Position
1
2
3
4
5
6
7
8
9
10
11
Weight
10
1
3
5
7
9
11
13
15
17
19
Example, to check the validity of ABN 53 004 085 616
5
3
0
0
4
0
8
5
6
1
6
Subtract 1 from first (left) digit to give new number
4
3
0
0
4
0
8
5
6
1
6
Apply weighting factor
10
1
3
5
7
9
11
13
15
17
19
(4x10)+(3x1)+(0x3)+(0x5)+(4x7)+(0x9)+(8x11)+(5x13)+(6x15)+(1x17)+ (6x19)
40+3+0+0+28+0+88+65+90+17+114
445/89 = 5 remainder 0
The remainder is zero so the number is valid.
Here is a C# Validation:
public static bool ValidateABN(string abn)
{
bool isValid = false;
int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
int weightedSum = 0;
//ABN must not contain spaces, comma's or hypens
abn = StripNonDigitData(abn);
//ABN must be 11 digits long
if (!string.IsNullOrEmpty(abn) & Regex.IsMatch(abn, "^\\d{11}$"))
{
//Rules: 1,2,3
for (int i = 0; i <= weight.Length - 1; i++)
{
weightedSum += (int.Parse(abn[i].ToString()) - ((i == 0 ? 1 : 0))) * weight[i];
}
//Rules: 4,5
return ((weightedSum % 89) == 0);
}
return isValid;
}
public static string StripNonDigitData(string input)
{
StringBuilder output = new StringBuilder("");
foreach (char c in input)
{
if (char.IsDigit(c))
{
output.Append(c);
}
}
return output.ToString();
}
And a VB.Net Validation:
Public Shared Function ValidateABN(ByVal abn As String) As Boolean
Dim isValid As Boolean = False
Dim weight() As Integer = {10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
Dim weightedSum As Integer = 0
'ABN must not contain spaces, comma's or hypens
abn = StripNonDigitData(abn)
'ABN must be 11 digits long
If Not String.IsNullOrEmpty(abn) And Regex.IsMatch(abn, "^\d{11}$") Then
'Rules: 1,2,3
For i As Integer = 0 To weight.Length - 1
weightedSum += (Integer.Parse(abn(i).ToString()) - (IIf(i = 0, 1, 0))) * weight(i)
Next
'Rules: 4,5
Return ((weightedSum Mod 89) = 0)
End If
Return isValid
End Function
Public Shared Function StripNonDigitData(ByVal input As String) As String
Dim output As New StringBuilder("")
For Each c As Char In input
If Char.IsDigit(c) Then
output.Append(c)
End If
Next
Return output.ToString
End Function
// return false if not correct AU vat format
function ABNValidation (val) {
val = val.replace(/[^0-9]/g, '');
let weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
if (val.length === 11) {
let sum = 0;
weights.forEach(function(weight, position) {
let digit = val[position] - (position ? 0 : 1);
sum += weight * digit;
});
return sum % 89 == 0;
}
return false;
}
Another JavaScript version:
/**
* validate ABN
* #param {string} abn
* #return {boolean} is ABN number valid
*
* 0. ABN must be 11 digits long
* 1. Subtract 1 from the first (left) digit to give a new eleven digit number
* 2. Multiply each of the digits in this new number by its weighting factor
* 3. Sum the resulting 11 products
* 4. Divide the total by 89, noting the remainder
* 5. If the remainder is zero the number is valid
*/
var validateABN = function(abn){
var isValid = true;
//remove all spaces
abn = abn.replace(/\s/g, '');
//0. ABN must be 11 digits long
isValid &= abn && /^\d{11}$/.test(abn);
if(isValid){
var weightedSum = 0;
var weight = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
//Rules: 1,2,3
for (var i = 0; i < weight.length; i++) {
weightedSum += (parseInt(abn[i]) - ((i === 0) ? 1 : 0)) * weight[i];
}
//Rules: 4,5
isValid &= ((weightedSum % 89) === 0);
}
return isValid;
};
//tests
console.log(validateABN('51824753556'));
console.log(validateABN('51 824 753 556'));
console.log(validateABN('51824744556'));
Have you read up on regexes? This is as straightforward as it gets.
^[0-9 ]+$
For those using AngularJS, I have written a directive to do ABN validation:
var app = angular.module("demoapp", ["input-abn-directive"]);
and
<input type="abn" name="inputAbn" ng-model="modelAbn">
https://github.com/jasadams/input-abn-directive
TESTED ABN Javascript implementation
These are WORKING ABNs:
33 051 775 556 should work
14 069 979 460 should work
24 302 976 253 should work
These are INVALID ABNs:
65 065 987 968 should fail
12 345 678 965 should fail
function checkABN(value) {
let weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
let abn = value.replace(/\D/g, ''); // strip non numeric chars
if (abn.length != 11) {
return false;
}
let abnFirstDigit = parseInt(abn.charAt(0)) - 1; //subtract 1 from first digit
abn = abnFirstDigit + abn.substring(1); // replace first digit with the substracted value
let total = 0;
for (let i = 0; i < 11; i++)
total += weights[i] * abn.charAt(i);
if (total == 0 || total % 89 != 0) {
return false;
} else {
return true;
}
}
The one from http://www.mathgen.ch/codes/abn.html has issues,
it won't allow 14 069 979 460 (which should be valid)
JavaScript version:
function checkABN(str) {
if (!str || str.length !== 11) {
return false;
}
var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
checksum = str.split('').map(Number).reduce(
function(total, digit, index) {
if (!index) {
digit--;
}
return total + (digit * weights[index]);
},
0
);
if (!checksum || checksum % 89 !== 0) {
return false;
}
return true;
}
Answer to the Question
A lot of answers will include the regex [0-9]{11} or similar, which is sufficient for most situations, but none of the answers take into account the fact that ABNs cannot start with a zero.
The simplest regex available for determining an ABN, that takes into account the correct number of digits, any number of spaces and the non-zero leading digit, is: ^\s*[1-9](\s*\d){10}\s*$.
Standard Format
The standard number format is the 2,3,3,3 digit grouping pattern, as the first two digits form the check digits and the other 9 is the actual identifier.
The regex for specifically checking this format with or without spaces is ^[1-9]\d(\s?\d{3}){3}$.
This format can be easily mimicked using the PHP number_format function.
number_format( '12123123123', 0, '', ' ' ) === '12 123 123 123';
Actual Validation
Any of the other answers relating to actual validation should be mostly valid, but not 100% unless there is an additional check to ensure the number does not start with a zero. Expanding upon the current top rated answer, one additional check just needs to be made.
if ( strlen( $abn ) === 11 && $abn[0] > 0 ) { // or $abn > 9999999999
// ...
}
SQL Server solution - in a single case statement
As described in other answers, a single regular expression is unlikely to be able to validate an ABN. The case statement below can be used in Microsoft SQL Server to validate an ABN. You might like to create a persisted computed column on your table that contains the ABN field. Per comments below, if you return integers instead of text descriptions, then you can use such a field for relatively straightforward comparisons:
1 = valid
0 = invalid (wrong length)
-1 = invalid (fails the pattern check)
allowing you to use expressions like:
where abnValid = 1 -- match all valid records
where abnValid < 1 -- match all invalid records
The logic below for validating ABNs is based on Jeremy Thompson's answer here. The statement below is verbose and inefficient - so that it fits a single case statement so that you can use it with a persisted computed column - meaning the performance hit will be on update, not on select. It assumes your abnColumn is character based - so add an explicit cast if you're using a numeric type. It ignores space characters in your source data and assumes the first number of an ABN will not be zero.
case
when len(ltrim(rtrim(replace(abnColumn,' ','')))) <> 11 then 'Wrong length' -- or perhaps 0
when
(
((try_cast(left(ltrim(rtrim(replace(abnColumn,' ',''))),1) as int)-1)*10) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),10),1) as int)*1) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),9),1) as int)*3) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),8),1) as int)*5) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),7),1) as int)*7) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),6),1) as int)*9) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),5),1) as int)*11) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),4),1) as int)*13) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),3),1) as int)*15) +
(try_cast(left(right(ltrim(rtrim(replace(abnColumn,' ',''))),2),1) as int)*17) +
(try_cast(right(ltrim(rtrim(replace(abnColumn,' ',''))),1) as int)*19)
) %89 <> 0 then 'Check pattern fail' -- or perhaps -1
else 'Valid' -- or perhaps 1
end as abnValidationCheck -- or perhaps abnValid
For those TypeScript with reactive forms and angular2+ out there out there:
this.abnForm = new FormGroup({
abn: new FormControl('', [
this.abnValidator(),
Validators.pattern('^(\d *?){11}$')])
});
abnValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const weights = [10,1,3,5,7,9,11,13,15,17,19];
const cleanedABN = control.value.replace(/\D/g, '');
let valid = false;
if (cleanedABN.length === 11) {
let sum = 0;
weights.forEach((weight, ind) => {
const digit = parseInt(cleanedABN[ind], 10) - (ind === 0 ? 1 : 0);
sum += weight * digit;
});
valid = sum % 89 === 0;
} else {
valid = false;
}
return valid ? null : { invalid: true };
};
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a problem:
I have a N (N <= 40). N is a length of sequence of zeroz and ones. How to find the number of sequences of zeros and ones in which there are no three "1" together?
Example:
N = 3, answer = 7
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
Here's a solution using a recursive function :
(PHP code here, but it's really simple)
$seq = '';
function tree ($node, $flag, $seq)
{
if ($flag == 3) { return 0; }
if ($node == 0) { echo $seq, ' '; return 0;}
$seq1 = $seq.'1';
$seq2 = $seq.'0';
tree($node-1, $flag+1, $seq1);
tree($node-1, 0, $seq2);
}
tree(8, 0, $seq);
I use a tree to go through all the possible sequences, and a flag to check how many 1 in a row.
If there is two 1 in a row, then the flag reaches 3, and the function is stopped for this branch.
If we reach a leaf of the tree (ie. $node = 0), then the sequence is displayed, and the function ends.
Else, the function explores the two sub-trees starting from the current node.
void tree ( int node, int flag, std::string seq)
{
std::string seq1 = seq;
std::string seq2 = seq;
if(flag ==3) { return; }
if(node ==0) { printf("%s\n",seq.c_str()); return;}
seq1 += '1';
seq2 += '0';
tree(node-1, flag+1, seq1);
tree(node-1, 0, seq2);
}
You can write a grammar for the (non-empty) strings of this language. It's designed so that each string appears exactly once.
S := 0 | 1 | 11 | 10 | 110 | 0S | 10S | 110S
Let a_i be the total number of strings of length i in S.
First, look at the number of strings of length 1 on both sides of the grammar rule. There's a_1 in S by definition which deals with the left-hand-side.
a_1 = 2
For a_2, on the right-hand-side we immediately get two strings of length 2 (11 and 10), plus another two from the 0S rule (00 and 01). This gives us:
a_2 = 2 + a_1 = 4
Similarly, for a_3, we get:
a_3 = 1 + a_2 + a_1 = 7
(So far so good, we've got the right solution 7 for the case where the strings are length three).
For i > 3, consider the number of strings of length i on both sides.
a_i = a_{i-1} + a_{i-2} + a_{i-3}
Now we've got a recurrence we can use. A quick check for a_4...
a_4 = a_1 + a_2 + a_3 = 2 + 4 + 7 = 13.
There's 16 strings of length 4 and three containing 111: 1110, 0111, 1111. So 13 looks right!
Here's some code in Python for the general case, using this recurrence.
def strings_without_111(n):
if n == 0: return 1
a = [2, 4, 7]
for _ in xrange(n - 1):
a = [a[1], a[2], a[0] + a[1] + a[2]]
return a[0]
This is a dp problem. I will explain the solution in a way so that it is easy to modify it to count the number of sequences having no sequence a0a1a2 in them(where ai is arbitrary binary value).
I will use 4 helper variables each counting the sequence up to a given length that are valid and end with 00, 01, 10, and 11 respectively. Name those c00, c01, c10, c11. It is pretty obvious that for length N = 2, those numbers are all 1:
int c00 = 1;
int c01 = 1;
int c10 = 1;
int c11 = 1;
Now assuming we have counted the sequences up to a given length k we count the sequences in the four groups for length k + 1 in the following manner:
int new_c00 = c10 + c00;
int new_c01 = c10 + c00;
int new_c10 = c01 + c11;
int new_c11 = c01;
The logic above is pretty simple - if we append a 0 to either a sequence of length k ending at 0 0 or ending at 1 0 we end up with a new sequence of length k + 1 and ending with 0 0 and so on for the other equations above.
Note that c11 is not added to the number of sequences ending with 1 1 and with length k + 1. That is because if we append 1 to a sequence ending with 1 1 we will end up with an invalid sequence( ending at 1 1 1).
Here is a complete solution for your case:
int c00 = 1;
int c01 = 1;
int c10 = 1;
int c11 = 1;
for (int i = 0; i < n - 2; ++i) {
int new_c00 = c10 + c00;
int new_c01 = c10 + c00;
int new_c10 = c01 + c11;
int new_c11 = c01;
c00 = new_c00;
c01 = new_c01;
c10 = new_c10;
c11 = new_c11;
}
// total valid sequences of length n
int result = c00 + c01 + c10 + c11;
cout << result << endl;
Also you will have to take special care for the case when N < 2, because the above solution does not handle that correctly.
To find a number of all possible sequences for N bits are easy. It is 2^N.
To find all sequences contains 111 a bit harder.
Assume N=3 then Count = 1
111
Assume N=4 then Count = 3
0111
1110
1111
Assume N=5 then Count = 8
11100
11101
11110
11111
01110
01111
00111
10111
If you write simple simulation program it yields 1 3 8 20 47 107 ...
Subtract 2^n - count(n) = 2 4 7 13 24 44 81 149...
Google it and it gives OEIS sequence, known as tribonacci numbers. Solved by simple recurrent equation:
a(n) = a(n - 1) + a(n - 2) + a(n - 3)
I have three integer variables, that can take only the values 0, 1 and 2. I want to distinguish what combination of all three numbers I have, ordering doesn't count. Let's say the variables are called x, y and z. Then x=1, y=0, z=0 and x=0, y=1, z=0 and x=0, y=0, z=1 are all the same number in this case, I will refer to this combination as 001.
Now there are a hundred ways how to do this, but I am asking for an elegant solution, be it only for educational purposes.
I thought about bitwise shifting 001 by the amount of the value:
001 << 0 = 1
001 << 1 = 2
001 << 2 = 4
But then the numbers 002 and 111 would both give 6.
The shift idea is good, but you need 2 bits to count to 3. So try shifting by twice the number of bits:
1 << (2*0) = 1
1 << (2*1) = 4
1 << (2*2) = 16
Add these for all 3 numbers, and the first 2 bits will count how many 0 you have, the second 2 bits will count how many 1 and the third 2 bits will count how many 2.
Edit although the result is 6 bit long (2 bits per number option 0,1,2), you only need the lowest 4 bits for a unique identifier - as if you know how many 0 and 1 you have, then the number of 2 is determined also.
So instead of doing
res = 1<<(2*x);
res+= 1<<(2*y);
res+= 1<<(2*z);
you can do
res = x*x;
res+= y*y;
res+= z*z;
because then
0*0 = 0 // doesn't change result. We don't count 0
1*1 = 1 // we count the number of 1 in the 2 lower bits
2*2 = 4 // we count the number of 2 in the 2 higher bits
hence using only 4 bits instead of 6.
When the number of distinct possibilities is small, using a lookup table could be used.
First, number all possible combinations of three digits, like this:
Combinations N Indexes
------------- - ------
000 0 0
001, 010, 100 1 1, 3, 9
002, 020, 200 2 2, 6, 18
011, 101, 110 3 4, 10, 12
012, 021, 102, 120, 201, 210 4 5, 7, 11, 15, 19, 21
022, 202, 220 5 8, 20, 24
111 6 13
112, 121, 211 7 14, 16, 22
122, 212, 221 8 17, 23, 25
222 9 26
The first column shows identical combinations; the second column shows the number of the combination (I assigned them arbitrarily); the third column shows the indexes of each combination, computed as 9*<first digit> + 3*<second digit> + <third digit>.
Next, build a look-up table for each of these ten combinations, using this expression as an index:
9*a + 3*b + c
where a, b, and c are the three numbers that you have. The table would look like this:
int lookup[] = {
0, 1, 2, 1, 3, 4, 2, 4, 5, 1
, 3, 4, 3, 6, 7, 4, 7, 8, 2, 4
, 5, 4, 7, 8, 5, 8, 9
};
This is a rewrite of the first table, with values at the indexes corresponding to the value in the column N. For example, combination number 1 is founds at indexes 1, 3, and 9; combination 2 is at indexes 2, 6, and 18, and so on.
To obtain the number of the combination, simply check
int combNumber = lookup[9*a + 3*b + c];
For such small numbers, it would be easiest to just check them individually, instead of trying to be fancy, eg:
bool hasZero = false;
bool hasOne = false;
bool hasTwo = false;
// given: char* number or char[] number...
for(int i = 0; i < 3; ++i)
{
switch (number[i])
{
case '0': hasZero = true; break;
case '1': hasOne = true; break;
case '2': hasTwo = true; break;
default: /* error! */ break;
}
}
If I understand you correctly, you have some sequence of numbers that can either be 1, 2, or 3, where the permutation of them doesn't matter (just the different combinations).
That being the case:
std::vector<int> v{1, 2, 3};
std::sort(v.begin(), v.end());
That will keep all of the different combinations properly aligned, and you could easily write a loop to test for equality.
Alternatively, you could use a std::array<int, N> (where N is the number of possible values - in this case 3).
std::array<int, 3> a;
Where you would set a[0] equal to the number of 1s you have, a[1] equal to the number of '2's, etc.
// if your string is 111
a[0] = 3;
// if your string is 110 or 011
a[0] = 2;
// if your string is 100 or 010 or 001
a[0] = 1;
// if your string is 120
a[0] = 1;
a[1] = 1;
// if your string is 123
a[0] = 1;
a[1] = 1;
a[2] = 1;
If you are looking to store it in a single 32-bit integer:
unsigned long x = 1; // number of 1's in your string
unsigned long y = 1; // number of 2's in your string
unsigned long z = 1; // number of 3's in your string
unsigned long result = x | y << 8 | z << 16;
To retrieve the number of each, you would do
unsigned long x = result & 0x000000FF;
unsigned long y = (result >> 8) & 0x000000FF;
unsigned long z = (result >> 16) & 0x000000FF;
This is very similar to what happens in the RBG macros.
int n[3]={0,0,0};
++n[x];
++n[y];
++n[z];
Now, in the n array, you have a unique ordered combination of values for each unique unordered combination of x,y,z.
For example, both x=1,y=0,z=0 and x=0,y=0,z=1 will give you n={2,1,0}
consider that
0 -- is the first
1 -- is the second
2 -- is the third
.....
9 -- is the 10th
11 -- is the 11th
what is an efficient algorithm to find the nth palindromic number?
I'm assuming that 0110 is not a palindrome, as it is 110.
I could spend a lot of words on describing, but this table should be enough:
#Digits #Pal. Notes
0 1 "0" only
1 9 x with x = 1..9
2 9 xx with x = 1..9
3 90 xyx with xy = 10..99 (in other words: x = 1..9, y = 0..9)
4 90 xyyx with xy = 10..99
5 900 xyzyx with xyz = 100..999
6 900 and so on...
The (nonzero) palindromes with even number of digits start at p(11) = 11, p(110) = 1001, p(1100) = 100'001,.... They are constructed by taking the index n - 10^L, where L=floor(log10(n)), and append the reversal of this number: p(1101) = 101|101, p(1102) = 102|201, ..., p(1999) = 999|999, etc. This case must be considered for indices n >= 1.1*10^L but n < 2*10^L.
When n >= 2*10^L, we get the palindromes with odd number of digits, which start with p(2) = 1, p(20) = 101, p(200) = 10001 etc., and can be constructed the same way, using again n - 10^L with L=floor(log10(n)), and appending the reversal of that number, now without its last digit: p(21) = 11|1, p(22) = 12|1, ..., p(99) = 89|8, ....
When n < 1.1*10^L, subtract 1 from L to be in the correct setting with n >= 2*10^L for the case of an odd number of digits.
This yields the simple algorithm:
p(n) = { L = logint(n,10);
P = 10^(L - [1 < n < 1.1*10^L]); /* avoid exponent -1 for n=1 */
n -= P;
RETURN( n * 10^L + reverse( n \ 10^[n >= P] ))
}
where [...] is 1 if ... is true, 0 else, and \ is integer division.
(The expression n \ 10^[...] is equivalent to: if ... then n\10 else n.)
(I added the condition n > 1 in the exponent to avoid P = 10^(-1) for n=0. If you use integer types, you don't need this. Another choice it to put max(...,0) as exponent in P, or use if n=1 then return(0) right at the start. Also notice that you don't need L after assigning P, so you could use the same variable for both.)
How is this code working for multiple of 5
bool isMultipleof5(int n)
{
/* If n is a multiple of 5 then we make sure that last
digit of n is 0 */
if ( (n&1) == 1 )
n <<= 1;
float x = n;
x = ( (int)(x*0.1) )*10;
/* If last digit of n is 0 then n will be equal to (int)x */
if ( (int)x == n )
return true;
return false;
}
It first makes n divisable by 2.
Next, it checks if it is divisable by 10 by multiplying with 0.1 and again with 10. The idea that if it is divisable by 10, you will get back to the original, and only then.
So, if the modifies n is divisable by 10 - it is certainly divisable by 5 as well, and since modified n is always divisable by 2, if it is divisable by 5 it will be divisable by 10, and the algorithm works.
NOTE: This is very unsuggested and especially might break with large values due to floating point precision issues. using the % operator should be prefered: return (n % 5) == 0
This is how the code works with some examples.
if ( (n&1) == 1 ) //Checks if the number is odd
n <<= 1; //Multiplies the number by 2 if odd
x = ( (int)(x * 0.1) //Divides the number 10 then truncates any decimal places
* 10 ) //Multiplies it back by 10
if ( (int)x == n ) //If the floating point value equals the (semi) original value its divisible by 5
return true;
return false; //Other wise false
Example:
15 & 1 == 1 //15 is odd
15 <<= 1; //n is now 30
30 / 10 = 3;
3 * 10 = 30; //x is now 30
30 == 30 //15 is a multiple of 5
17 & 1 == 1 //17 is odd
17 <<= 1; //n is now 34
34 / 10 = 3.4;
((int)3.4 = 3) * 10 = 30; //x is now 30
30 != 34 //17 is not a multiple of 5.
As others said though just simply use the mod operator %.
This is how it works:
Double the number. Now anything ending in 5 will be divisible 10 (and also divisible by 5). n <<= 1; (the check for oddness is unnecessary (n&1) == 1)
Divide it by 10, and cast away the fractional part. (int)(x*0.1)
Multiply it by 10, so now we have the same number as in step 1 only if the number in step 1 was already divisible by 10.
The use of floating point to divide by 10 makes this algorithm dangerous and probably incorrect for large values.
Try this
bool isMultipleof5(int n)
{
return (n%5) == 0;
}
A simpler way would be
bool isMultipleof5(int n)
{
return 0 == ( n % 5 ) ;
}
#define IS_MULTIPLE_OF_5(n) (((n)%5) ? 0 : 1)
I'd agree that (n % 5) == 0 would be an ideal solution, but that wasn't really the question.
This code works because it first checks if the input is odd. If it is, it multiplies by two. Since all odd multiples of 5 end with a 5, multiplying by 2 gives a number that ends with 0.
Then it checks if the last digit is 0. This can only happen if it started as a 0 (i.e. was even, we didn't change it) or if it was odd and ended in a 5 (we multiplied by 2). So, if it ends in 0 then the input must have been divisible by 5.
I'd add that this is also an awkward way to check the value of the last digit. I'd suggest n % 10 == 0 instead, but like others mentioned... you could have just used n % 5 == 0 in the first place ;).
I'm reading about permutations and I'm interested in ranking/unranking methods.
From the abstract of a paper:
A ranking function for the permutations on n symbols assigns a unique
integer in the range [0, n! - 1] to each of the n! permutations. The corresponding
unranking function is the inverse: given an integer between 0 and n! - 1, the
value of the function is the permutation having this rank.
I made a ranking and an unranking function in C++ using next_permutation. But this isn't practical for n>8. I'm looking for a faster method and factoradics seem to be quite popular.
But I'm not sure if this also works with duplicates. So what would be a good way to rank/unrank permutations with duplicates?
I will cover one half of your question in this answer - 'unranking'. The goal is to find the lexicographically 'K'th permutation of an ordered string [abcd...] efficiently.
We need to understand Factorial Number System (factoradics) for this. A factorial number system uses factorial values instead of powers of numbers (binary system uses powers of 2, decimal uses powers of 10) to denote place-values (or base).
The place values (base) are –
5!= 120 4!= 24 3!=6 2!= 2 1!=1 0!=1 etc..
The digit in the zeroth place is always 0. The digit in the first place (with base = 1!) can be 0 or 1. The digit in the second place (with base 2!) can be 0,1 or 2 and so on. Generally speaking, the digit at nth place can take any value between 0-n.
First few numbers represented as factoradics-
0 -> 0 = 0*0!
1 -> 10 = 1*1! + 0*0!
2 -> 100 = 1*2! + 0*1! + 0*0!
3 -> 110 = 1*2! + 1*1! + 0*0!
4 -> 200 = 2*2! + 0*1! + 0*0!
5 -> 210 = 2*2! + 1*1! + 0*0!
6 -> 1000 = 1*3! + 0*2! + 0*1! + 0*0!
7 -> 1010 = 1*3! + 0*2! + 1*1! + 0*0!
8 -> 1100 = 1*3! + 1*2! + 0*1! + 0*0!
9 -> 1110
10-> 1200
There is a direct relationship between n-th lexicographical permutation of a string and its factoradic representation.
For example, here are the permutations of the string “abcd”.
0 abcd 6 bacd 12 cabd 18 dabc
1 abdc 7 badc 13 cadb 19 dacb
2 acbd 8 bcad 14 cbad 20 dbac
3 acdb 9 bcda 15 cbda 21 dbca
4 adbc 10 bdac 16 cdab 22 dcab
5 adcb 11 bdca 17 cdba 23 dcba
We can see a pattern here, if observed carefully. The first letter changes after every 6-th (3!) permutation. The second letter changes after 2(2!) permutation. The third letter changed after every (1!) permutation and the fourth letter changes after every (0!) permutation. We can use this relation to directly find the n-th permutation.
Once we represent n in factoradic representation, we consider each digit in it and add a character from the given string to the output. If we need to find the 14-th permutation of ‘abcd’. 14 in factoradics -> 2100.
Start with the first digit ->2, String is ‘abcd’. Assuming the index starts at 0, take the element at position 2, from the string and add it to the Output.
Output String
c abd
2 012
The next digit -> 1.String is now ‘abd’. Again, pluck the character at position 1 and add it to the Output.
Output String
cb ad
21 01
Next digit -> 0. String is ‘ad’. Add the character at position 1 to the Output.
Output String
cba d
210 0
Next digit -> 0. String is ‘d’. Add the character at position 0 to the Output.
Output String
cbad ''
2100
To convert a given number to Factorial Number System,successively divide the number by 1,2,3,4,5 and so on until the quotient becomes zero. The reminders at each step forms the factoradic representation.
For eg, to convert 349 to factoradic,
Quotient Reminder Factorial Representation
349/1 349 0 0
349/2 174 1 10
174/3 58 0 010
58/4 14 2 2010
14/5 2 4 42010
2/6 0 2 242010
Factoradic representation of 349 is 242010.
One way is to rank and unrank the choice of indices by a particular group of equal numbers, e.g.,
def choose(n, k):
c = 1
for f in xrange(1, k + 1):
c = (c * (n - f + 1)) // f
return c
def rank_choice(S):
k = len(S)
r = 0
j = k - 1
for n in S:
for i in xrange(j, n):
r += choose(i, j)
j -= 1
return r
def unrank_choice(k, r):
S = []
for j in xrange(k - 1, -1, -1):
n = j
while r >= choose(n, j):
r -= choose(n, j)
n += 1
S.append(n)
return S
def rank_perm(P):
P = list(P)
r = 0
for n in xrange(max(P), -1, -1):
S = []
for i, p in enumerate(P):
if p == n:
S.append(i)
S.reverse()
for i in S:
del P[i]
r *= choose(len(P) + len(S), len(S))
r += rank_choice(S)
return r
def unrank_perm(M, r):
P = []
for n, m in enumerate(M):
S = unrank_choice(m, r % choose(len(P) + m, m))
r //= choose(len(P) + m, m)
S.reverse()
for i in S:
P.insert(i, n)
return tuple(P)
if __name__ == '__main__':
for i in xrange(60):
print rank_perm(unrank_perm([2, 3, 1], i))
For large n-s you need arbitrary precision library like GMP.
this is my previous post for an unranking function written in python, I think it's readable, almost like a pseudocode, there is also some explanation in the comments: Given a list of elements in lexicographical order (i.e. ['a', 'b', 'c', 'd']), find the nth permutation - Average time to solve?
based on this you should be able to figure out the ranking function, it's basically the same logic ;)
Java, from https://github.com/timtiemens/permute/blob/master/src/main/java/permute/PermuteUtil.java (my public domain code, minus the error checking):
public class PermuteUtil {
public <T> List<T> nthPermutation(List<T> original, final BigInteger permutationNumber) {
final int size = original.size();
// the return list:
List<T> ret = new ArrayList<>();
// local mutable copy of the original list:
List<T> numbers = new ArrayList<>(original);
// Our input permutationNumber is [1,N!], but array indexes are [0,N!-1], so subtract one:
BigInteger permNum = permutationNumber.subtract(BigInteger.ONE);
for (int i = 1; i <= size; i++) {
BigInteger factorialNminusI = factorial(size - i);
// casting to integer is ok here, because even though permNum _could_ be big,
// the factorialNminusI is _always_ big
int j = permNum.divide(factorialNminusI).intValue();
permNum = permNum.mod(factorialNminusI);
// remove item at index j, and put it in the return list at the end
T item = numbers.remove(j);
ret.add(item);
}
return ret;
}
}