How do you find the rightmost digit of an integer? - math

Assume that an int variable x that has already been declared,
write an expression whose value is the last (rightmost) digit of x.
I know the answer is x%10, but why is that the expression that reveals the rightmost digit?

x % 10 looks like the correct answer. But is not.
-2 % 10 is either -2 or 8 depending on language/implementation/whatever. And neither is actually "last digit".
So the correct answer is abs(x) % 10.

It's Math
When you use the '%' operator you are asking for "What is the remainder after dividing by that number" So if I have 28 % 10 I'm saying "What is the remainder of 28 divided by 10". This, of course, would give me 8. Getting the remainder of a number divided by 10 only leaves you with the ones digit (right most number).
We use a Decimal / Base 10 number system. So if you use 10 you will always get remainders in between 0-9.
In Java, '%' is the remainder operator and works the way described above.
Summary of Operators in Java

Related

F#: integer (%) integer - Is Calculated How?

So in my text book there is this example of a recursive function using f#
let rec gcd = function
| (0,n) -> n
| (m,n) -> gcd(n % m,m);;
with this function my text book gives the example by executing:
gcd(36,116);;
and since the m = 36 and not 0 then it ofcourse goes for the second clause like this:
gcd(116 % 36,36)
gcd(8,36)
gcd(36 % 8,8)
gcd(4,8)
gcd(8 % 4,4)
gcd(0,4)
and now hits the first clause stating this entire thing is = 4.
What i don't get is this (%)percentage sign/operator or whatever it is called in this connection. for an instance i don't get how
116 % 36 = 8
I have turned this so many times in my head now and I can't figure how this can turn into 8?
I know this is probably a silly question for those of you who knows this but I would very much appreciate your help the same.
% is a questionable version of modulo, which is the remainder of an integer division.
In the positive, you can think of % as the remainder of the division. See for example Wikipedia on Euclidean Divison. Consider 9 % 4: 4 fits into 9 twice. But two times four is only eight. Thus, there is a remainder of one.
If there are negative operands, % effectively ignores the signs to calculate the remainder and then uses the sign of the dividend as the sign of the result. This corresponds to the remainder of an integer division that rounds to zero, i.e. -2 / 3 = 0.
This is a mathematically unusual definition of division and remainder that has some bad properties. Normally, when calculating modulo n, adding or subtracting n on the input has no effect. Not so for this operator: 2 % 3 is not equal to (2 - 3) % 3.
I usually have the following defined to get useful remainders when there are negative operands:
/// Euclidean remainder, the proper modulo operation
let inline (%!) a b = (a % b + b) % b
So far, this operator was valid for all cases I have encountered where a modulo was needed, while the raw % repeatedly wasn't. For example:
When filling rows and columns from a single index, you could calculate rowNumber = index / nCols and colNumber = index % nCols. But if index and colNumber can be negative, this mapping becomes invalid, while Euclidean division and remainder remain valid.
If you want to normalize an angle to (0, 2pi), angle %! (2. * System.Math.PI) does the job, while the "normal" % might give you a headache.
Because
116 / 36 = 3
116 - (3*36) = 8
Basically, the % operator, known as the modulo operator will divide a number by other and give the rest if it can't divide any longer. Usually, the first time you would use it to understand it would be if you want to see if a number is even or odd by doing something like this in f#
let firstUsageModulo = 55 %2 =0 // false because leaves 1 not 0
When it leaves 8 the first time means that it divided you 116 with 36 and the closest integer was 8 to give.
Just to help you in future with similar problems: in IDEs such as Xamarin Studio and Visual Studio, if you hover the mouse cursor over an operator such as % you should get a tooltip, thus:
Module operator tool tip
Even if you don't understand the tool tip directly, it'll give you something to google.

How to determine that remaining of dividing number X by number Y is zero using regular expression

i want to know is it possible to validate that deviding two number has remaining zero in result or not?
for example dividing number 4 on number two has zero in remaining.
4/2=0 (this is true)
but 4/3=1 (this is not true)
is there any expression for validation such case?
Better Question :
Is There any validation expression to validate this sentence ?
Remainder is zero
thank you
You can use a Modulo operator. The modulo operation finds the remainder of division of one number by another
y mod x
5 mod 2 =1 (2x2=4, 5-4=1)
9 mod 3 = 0 (3*3=9)
You can think of it, how many times does x fit in y and then take the remainder.
In computing the modulo operator is integrated in most programming languages, along with division, substraction etc. Check modulo and then your language on google (probably its mod).
This is called the modulo function. It essentially gives the remainder of a division of two integer number. So you can test for the modulo funtion returning zero. For example, in Python you would write
if a % b == 0:
# a can be divided by b with zero remainder

Regular expression for 3 decimal point

I need a regular expression that satisfy these rules:
The maximum number of decimal point is 3 but a number with no decimal point (e.g 12) should be accepted too
the value must be at least 0
the value must be less or equal to 99999999999.999
the radix point is DOT (e.g 2.5, not 2,5)
Sample of valid numbers:
0
2
0.4
78784764.23
45.232
Sample of invalid numbers:
-2
123456789522144
84.2564
I found an example here (http://forums.asp.net/t/1642501.aspx) and have managed to modify it a little bit to make 0 as the minimum value, 99999999999.999 as the maximum value and to accept only DOT as radix point. Here's my modified regex:
^\-?(([0-9]\d?|0\d{1,2})((\.)\d{0,2})?|99999999999.999((\.)0{1,2})?)$
However, I still have problem with the 3 decimal point and it is rather unstable. Can anyone help me on this since I'm basically illiterate when it comes to regex?
Thanks.
EDITED:
I'm using ASP Regular Expression Validator
This is not that difficult:
^[0-9]{1,11}(?:\.[0-9]{1,3})?$
Explanation:
^ # Start of string
[0-9]{1,11} # Match 1-11 digits (i. e. 0-99999999999)
(?: # Try to match...
\. # a decimal point
[0-9]{1,3} # followed by one to three digits (i. e. 0-999)
)? # ...optionally
$ # End of string

Probability of 3-character string appearing in a randomly generated password

If you have a randomly generated password, consisting of only alphanumeric characters, of length 12, and the comparison is case insensitive (i.e. 'A' == 'a'), what is the probability that one specific string of length 3 (e.g. 'ABC') will appear in that password?
I know the number of total possible combinations is (26+10)^12, but beyond that, I'm a little lost. An explanation of the math would also be most helpful.
The string "abc" can appear in the first position, making the string look like this:
abcXXXXXXXXX
...where the X's can be any letter or number. There are (26 + 10)^9 such strings.
It can appear in the second position, making the string look like:
XabcXXXXXXXX
And there are (26 + 10)^9 such strings also.
Since "abc" can appear at anywhere from the first through 10th positions, there are 10*36^9 such strings.
But this overcounts, because it counts (for instance) strings like this twice:
abcXXXabcXXX
So we need to count all of the strings like this and subtract them off of our total.
Since there are 6 X's in this pattern, there are 36^6 strings that match this pattern.
I get 7+6+5+4+3+2+1 = 28 patterns like this. (If the first "abc" is at the beginning, the second can be in any of 7 places. If the first "abc" is in the second place, the second can be in any of 6 places. And so on.)
So subtract off 28*36^6.
...but that subtracts off too much, because it subtracted off strings like this three times instead of just once:
abcXabcXabcX
So we have to add back in the strings like this, twice. I get 4+3+2+1 + 3+2+1 + 2+1 + 1 = 20 of these patterns, meaning we have to add back in 2*20*(36^3).
But that math counted this string four times:
abcabcabcabc
...so we have to subtract off 3.
Final answer:
10*36^9 - 28*36^6 + 2*20*(36^3) - 3
Divide that by 36^12 to get your probability.
See also the Inclusion-Exclusion Principle. And let me know if I made an error in my counting.
If A is not equal to C, the probability P(n) of ABC occuring in a string of length n (assuming every alphanumeric symbol is equally likely) is
P(n)=P(n-1)+P(3)[1-P(n-3)]
where
P(0)=P(1)=P(2)=0 and P(3)=1/(36)^3
To expand on Paul R's answer. Probability (for equally likely outcomes) is the number of possible outcomes of your event divided by the total number of possible outcomes.
There are 10 possible places where a string of length 3 can be found in a string of length 12. And there are 9 more spots that can be filled with any other alphanumeric characters, which leads to 36^9 possibilities. So the number of possible outcomes of your event is 10 * 36^9.
Divide that by your total number of outcomes 36^12. And your answer is 10 * 36^-3 = 0.000214
EDIT: This is not completely correct. In this solution, some cases are double counted. However they only form a very small contribution to the probability so this answer is still correct up to 11 decimal places. If you want the full answer, see Nemo's answer.

Using base X, how high can I count using Y characters?

I know that the total number of permutations for a given base is the factorial... so the total number of permutations of "abc" is 3! or 3x2x1 or 6.
Obviously I'm not sure of the terminology to properly phrase my question, but I would like to find the highest numbered permutation before the "length" of it's representation increases to X characters.
For example, Using a Base 62 'alphabet', I can represent integers up to 238327 before the representation uses 4 characters instead of 3. I'd like to know the math behind finding this out, given arbitrary values for Base and Length of representation.
Essentially, "using Base-X, how high can I count using Y characters?".
Assuming your numbers are positive and start at 0 then you can count from 0 to X^Y - 1.
As per your example above, 62^3 - 1 = 238328 - 1 = 238327.

Resources