Mathematical mind boggler, very confusing possible new mathematical breakthrough [closed] - math

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I'm not trying to make a joke here but I am very confused I been trying to figure this out for like 6 hours straight now got about 20 notepads opened up here, 15 calculators and I cant crunch it I'm always getting too much excess in the end.
Lets explain some variables here we got to work with.
Say we got
2566 min points / 2566 max points
0 min xp / 4835 max xp
There is 2 types of jobs that need to use both variables (points and xp)
Job (1) subtracts 32 points per click and adds 72 xp per click.
Job (2) subtracts 10 points per click and adds 14 xp per click.
I'm trying to figure out how to calculate the excess properly. So it would waste the minimal amount of Job(1)'s to still have enough points to do as much Job(2)'s as it possibly can and still reach max xp.
Thats the thing I dont want to run Job1's until there are no more points left because in doing so, the Job1's will exceeds the maximum XP (2566) and I will never get to do any Job2's.
I want to get the maximum possible Job2's in then using proper calculation achieve or overflow the MaxXP of 2566 with Job1's to always achieve max XP. Pretty much my situation is that I need to get 2566 MaxXP to be able to continue completing jobs. While keeping that in mind I want to place most priority on job2's and only use Job1's to achieve the necessary MaxXP of 2566 to reset the min points to max to redo the process all over. I am trying to automate this.
Here is my equations
amountOfJob1s = (minPoints / 32)
amountOfJob2s = (minPoints / 10)
excessXP = (amountOfJob1s * 72) - maxXP
if excessXP < 0 then break
Results
mustDoJob1s = ???
mustDoJob2s = ???
Thank you if anyone can help me figure this out so I can put a good equation here I'd appreciate it.
Either this is not mathematically possible or I just can't crunch it I do believe I have enough variables.

Let job1 be the amount of job1 and job2 be the amount of job2. We are left with two equations and two unknowns:
job1 * 32 + job2 * 10 = 2566
job1 * 72 + job2 * 14 = 4835
So:
job1 = 45.683...
job2 = 110.411...
Given job1 as the higher xp/point ratio and you wanna go over 4835 xp, round job1 up, compute job2 and round it down.
job1 = 46
job1 * 32 + job2 * 10 = 2566
job2 = 109.4
job2 = 109
Check:
job1 * 32 + job2 * 10 = 2562 points
job1 * 72 + job2 * 14 = 4838 xp
Done.
Two unknowns is hardly a 'new mathematical breakthrough' :)

I assume you want to get as much "XP" as possible, while spending no more than 2566 "points" by "clicking" an integer number of times {n1, n2} on each of two "jobs". Here is the answer in Mathematica:
In[8]:= Maximize[{72 n1 + 14 n2, n1 >= 0, n2 >= 0,
32 n1 + 10 n2 <= 2566}, {n1, n2}, Integers]
Out[8]= {5956, {n1 -> 80, n2 -> 0}}
Or, maybe you need to spend exactly 2566 points? Then the best you can do is:
In[9]:= Maximize[{72 n1 + 14 n2, n1 >= 0, n2 >= 0,
32 n1 + 10 n2 == 2566}, {n1, n2}, Integers]
Out[9]= {5714, {n1 -> 78, n2 -> 7}}
Is this what you wanted?

Let a be the number of Job 1 and b the number of Job 2.
XP = 72 a + 14 b
P = 32 a + 10 b
You appear to want to solve for a and b, such that XP <= 4835, P <= 2566 and b is as large as possible.
72 a + 14 b <= 4835
32 a + 10 b <= 2566
b will be largest when a = 0, i.e.
b <= 4835 ÷ 14, => b <= 345
b <= 2566 ÷ 10, => b <= 256
As b must be both below 345 and 256, it must be below 256.
Substitute back in:
72 a + 14 × 256 <= 4835, => a <= ( 4835 - 14 × 256 ) ÷ 72, => a <= 17
32 a + 10 × 256 <= 2566, => a <= ( 2566 - 10 × 256 ) ÷ 32, => a <= 0
so a = 0, XP is 2560 and points used is 3584.
Alternatively, you can solve for the closest satisfaction of the two inequalities
72 a + 14 b <= 4835 (1)
32 a + 10 b <= 2566 (2)
b <= ( 2566 - 32 a ) ÷ 10 (3) rearrange 2
72 a <= 4835 - 1.4 ( 2566 - 32 a ) (4) subst 3 into 1
27.2 a <= 1242.6
a <= 45.68
so choose a = 45 as the largest integer solution, giving b = 112, XP is 4808, points used is 2560
For either of these, there's no computer programming required; if the constants associated with the two jobs change, then the formulas change.
For harder to solve examples, the relevant area of mathematics is called linear programming

Related

Is there a mathematical formula for two's complement multiplication when dealing with overflow?

For instance, given a word size of 4 bits:
0b1001 * 0b0111 = 0b1111 // -7 * 7 = -1
0b0111 * 0b0111 = 0b0001 // 7 * 7 = 1
0b0111 * 0b0110 = 0b1010 // 7 * 6 = -6
0b1001 * 0b0110 = 0b0110 // -7 * 6 = 6
There's undoubtedly some modular arithmetic going on here, but the way you take mod seems to be quite inconsistent. Is there a neat mathematical formulation of two's complement multiplication?
The nice thing about twos complement is that addition, subtraction, and multiplication of signed operands are exactly the same operations, bit-for-bit, as the ones for unsigned operands, so the computer doesn't need to care whether you think of them as signed or not.
In terms of modular arithmetic as well, the operations mean exactly the same thing. With 4 bit words, when you say:
r = a * b;
You get r = a * b mod 16.
The only difference between signed and unsigned is the value we assign in our heads to the residues mod 16. If we think of the words as unsigned then we have values 0-15. But 15 = -1 mod 16, 14 = -2 mod 16, etc, and if we think of the words as signed, then we just think of the values -8 to 7 instead of 0 to 15.
The reminder operator % that you get in C, java, etc, is annoying in the way it handles negative numbers. If you wanted to express your 4-bit multiply using that operator in larger words, then you could say:
a * b = ( (a * b % 16) + 24 ) % 16 - 8
If the remainder operator worked "properly" so that -1 % 16 == 15, then you could write a * b = (a * b + 8) % 16 - 8

How to compute floor(log2(5**x)) without floating point arithmetic or long integer computation

The problem is how do we compute the integer value of floor(log2(5^x)) without floating point arithmetic or long integer computation? I'm looking for a simple, efficient and mathematically elegant way.
Observations:
The formula is just the number of bits in 5**x (plus 1)
Attempts:
I tried to simplify it to:
floor(x*log2(5))
In my use case, x is not extremely large, probably just 1-100. While an elegant formula that works for small values would suffice me, I would be interested in a formula/algorithm that works for any value of x
I'm making a reference software implementation of universal numbers (type III). I want to make everything easily convertible to microcode by purely using bitwise and basic operations. This is one of the formulas i need to simplify.
As you correctly note, log2(5**x) == x * log2(5). log2(5) is a constant, which can be approximated to 2.3219281.
However, floats aren't allowed per the question. Not an issue!
log2_5 = 23219281;
scale = 10000000; // note log2_5/scale is the actual value
result = x * log2_5;
output = (result - (result % scale)) / scale;
By reducing result by result % scale, dividing it by scale will be an integer division, not a float.
for a simple, efficient and mathematically elegant way... floor(x*log2(5))
Since x has integer values 1 to 100, various tests can to made to find the "best" that uses an integer multiply and a divide by power_of_2.
f(x) = x*a integer_divide power_of_2
For
f(x) = floor(x*log2(5)) = floor(x*some_float_c) the value of some_float_c is bounded by 100 minimum and maximums below.
x f(x) mn mx
f(x)/x (f(x) + 1)/x
1 2 2.00000 3.00000
2 4 2.00000 2.50000
3 6 2.00000 2.33333
...
59 136 2.30508 2.32203
...
87 202 2.32184 2.33333
...
98 227 2.31633 2.32653
99 229 2.31313 2.32323
100 232 2.32000 2.33000
The maximum min is 2.32184 and the minimum max is 2.32203, :
2.32184... <= some_float_c < 2.32203...
Since we cannot use float, find some_integer/some_power_of_2
2.32184... <= some_integer/some_power_of_2 < 2.32203...
ceil(some_power_of_2 * 2.32184...) <= some_integer < floor(some_power_of_2 * 2.32203...)
min max
2.32184 2.32203
2 5 4
4 10 9
8 19 18
...
1024 2378 2377
2048 4756 4755
4096 9511 9511 < first one where min <= max
8192 19021 19022
So 9511/4096 is the "simplest" and is a "best" candidate.
f(x) = (x*9511) integer_divide_by_power_of_2 4096
// In C
unsigned y = (x*9511u) >> 12;
Here is a very rough approximation, but it can help if you want to obtain it mentally
5^3 = 125
2^7 = 128
So for raising to the power of n:
5^n ~~ 2^(7n/3)
So 5^12 is near 2^28 might require up to 29 bits.
It's a bit overestimated because 2^7 > 5^3, so 28 bits are enough, a good usage is to simply round the fraction upper.
If I evaluate in Smalltalk:
(1 to: 50) reject: [:i | (5 raisedTo: i) highBit = (i*7/3) ceiling].
I get:
#(31 34 37 40 43 46 49)
You see that the very simple formulation works up to 5^30 which is not that bad.

Moving modulus operator to the other side of the equation

I have a mathematical problem that is part of my programming problem
I have a statement like
a = b%30;
How can I calculate b in terms of a?
I gave it a thought but couldn't figure it out.
By definition,
b == 30*n + a
for some integer n.
Note that there are multiple b values that can give you the same a:
>>> b = 31
>>> b % 30
1
>>> b = 61
>>> b % 30
1
First, obviously, there are in general several solutions for b for a given a.
If % is the remainder operator found in most programming languages, then the sign of a is critical. You know that this is a website for programming questions, right?
If |a|>=30, then there are no solutions
If a = 0, the solutions are the multiples of 30.
If 0 < a < 30, the solutions are all the b such that b = 30 * k + a for some positive integer k.
If -30 < a < 0, the solutions are all the b such that b = - (30 * k - a) for some positive integer k.

Lua level formula

I'm trying to calculate a players level depending on the players experience.
This is what I have in text:
Start at level 0 with 0 XP
To reach level 1, you need 50 XP.
One kill gives 10 XP.
So, 5 kills = 50 XP = level 1.
Now, level 2 should require 50 XP more than last level.
So, level 2 requires a total of 150 XP.
Level 1: 50 XP (5 kills total, 5 kills to go from level 0 to 1).
Level 2: 150 XP(15 kills total, 10 kills to go from level 1 to 2).
Level 3: 300 XP(30 kills total, 15 kills to go from level 2 to 3).
So far, all I got is this:
math.Round( xp / 50 )
This is 50 xp per level which isn't what I wanted; but I have really no idea where to go from here.
Simply put, I want each level to require 50 more XP than last level, and I need to get the level from an xp variable.
A quick math calculation shows that the experience point to reach level lv would be:
(1 + 2 + 3 + ... + lv) * 50
which equals:
lv * (lv + 1) / 2 * 50
So the question becomes to find the maximum non-negative integer lv that qualifies:
lv * (lv + 1) / 2 * 50 <= xp
That's the formula needs to be solved. And the math solution is:
lv <= (math.sqrt(xp * 4 / 25 + 1) - 1) / 2
Since you are looking for a non-negative integer, in the words of Lua, that's:
local lv = math.floor((math.sqrt(xp * 4 / 25 + 1) - 1) / 2)
You can wrap it to a function and give it a quick test like this:
function xp_to_lv(xp)
return math.floor((math.sqrt(xp * 4 / 25 + 1) - 1) / 2)
end
assert(0 == xp_to_lv(0))
assert(0 == xp_to_lv(49))
assert(1 == xp_to_lv(50))
assert(2 == xp_to_lv(260))
assert(3 == xp_to_lv(310))

How to find d, given p, q, and e in RSA?

I know I need to use the extended euclidean algorithm, but I'm not sure exactly what calculations I need to do. I have huge numbers. Thanks
Well, d is chosen such that d * e == 1 modulo (p-1)(q-1), so you could use the Euclidean algorithm for that (finding the modular multiplicative inverse).
If you are not interested in understanding the algorithm, you can just call BigInteger#modInverse directly.
d = e.modInverse(p_1.multiply(q_1))
Given that, p=11, q=7, e =17, n=77, φ (n) = 60 and d=?
First substitute values from the formula:-
ed mod φ (n) =1
17 d mod 60 = 1
The next step: – take the totient of n, which is 60 to your left hand side and [e] to your right hand side.
60 = 17
3rd step: – ask how many times 17 goes to 60. That is 3.5….. Ignore the remainder and take 3.
60 = 3(17)
Step 4: – now you need to balance this equation 60 = 3(17) such that left hand side equals to right hand side. How?
60 = 3(17) + 9 <== if you multiply 3 by 17 you get 51 then plus 9, that is 60. Which means both sides are now equal.
Step 5: – Now take 17 to your left hand side and 9 to your right hand side.
17 = 9
Step 6:- ask how many times 9 goes to 17. That is 1.8…….
17 = 1(9)
Step 7:- Step 4: – now you need to balance this 17 = 1(9)
17 = 1(9) + 8 <== if you multiply 1 by 9 you get 9 then plus 8, that is 17. Which means both sides are now equal.
Step 8:- again take 9 to your left hand side and 8 to your right hand side.
9 = 1(8)
9 = 1(8) + 1 <== once you reached +1 to balance your equation, you may stop and start doing back substitution.
Step A:-Last equation in step 8 which is 9 = 1(8) + 1 can be written as follows:
1.= 9 – 1(8)
Step B:-We know what is (8) by simple saying 8 = 17 – 1(9) from step 7. Now we can re-write step A as:-
1=9 -1(17 – 1(9)) <== here since 9=1(9) we can re-write as:-
1=1(9)-1(17) +1(9) <== group similar terms. In this case you add 1(9) with 1(9) – that is 2(9).
1=2(9)-1(17)
Step C: – We know what is (9) by simple saying 9 = 60 – 3(17) from step 4. Now we can re-write step B as:-
1=2(60-3(17) -1(17)
1=2(60)-6(17) -1(17) <== group similar terms. In this case you add 6(17) with 1(17) – that is 7(17).
1=2(60)-7(17) <== at this stage we can stop, nothing more to substitute, therefore take the value next 17. That is 7. Subtract it with the totient.
60-7=d
Then therefore the value of d= 53.
I just want to augment the Sidudozo's answer and clarify some important points.
First of all, what should we pass to Extended Euclidean Algorthim to compute d ?
Remember that ed mod φ(n) = 1 and cgd(e, φ(n)) = 1.
Knowing that the Extended Euclidean Algorthim is based on the formula cgd(a,b) = as + bt, hence cgd(e, φ(n)) = es + φ(n)t = 1, where d should be equal to s + φ(n) in order to satisfy the
ed mod φ(n) = 1 condition.
So, given the e=17 and φ(n)=60 (borrowed from the Sidudozo's answer), we substitute the corresponding values in the formula mentioned above:
cgd(e, φ(n)) = es + φ(n)t = 1 ⇔ 17s + 60t = 1.
At the end of the Sidudozo's answer we obtain s = -7. Thus d = s + φ(n) ⇔ d = -7 + 60 ⇒ d = 53.
Let's verify the results. The condition was ed mod φ(n) = 1.
Look 17 * 53 mod 60 = 1. Correct!
The approved answer by Thilo is incorrect as it uses Euler's totient function instead of Carmichael's totient function to find d. While the original method of RSA key generation uses Euler's function, d is typically derived using Carmichael's function instead for reasons I won't get into. The math needed to find the private exponent d given p q and e without any fancy notation would be as follows:
d = e^-1*mod(((p-1)/GCD(p-1,q-1))(q-1))
Why is this? Because d is defined in the relationship
de = 1*mod(λ(n))
Where λ(n) is Carmichael's function which is
λ(n)=lcm(p-1,q-1)
Which can be expanded to
λ(n)=((p-1)/GCD(p-1,q-1))(q-1)
So inserting this into the original expression that defines d we get
de = 1*mod(((p-1)/GCD(p-1,q-1))(q-1))
And just rearrange that to the final formula
d = e^-1*mod(((p-1)/GCD(p-1,q-1))(q-1))
More related information can be found here.
Here's the code for it, in python:
def inverse(a, n):
t, newt = 0, 1
r, newr = n, a
while newr:
quotient = r // newr # floor division
t, newt = newt, t - quotient * newt
r, newr = newr, r - quotient * newr
if r > 1:
return None # there's no solution
if t < 0:
t = t + n
return t
inverse(17, 60) # returns 53
adapted from pseudocode found in wiki: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Pseudocode
Simply use this formula,
d = (1+K(phi))/e. (Very useful when e and phi are small numbers)
Lets say, e = 3 and phi = 40
we assume K = 0, 1, 2... until your d value is not a decimal
assume K = 0, then
d = (1+0(40))/3 = 0. (if it is a decimal increase the K value, don't bother finding the exact value of the decimal)
assume K = 2, then
d = (1+2(40)/3) = 81/3 = 27
d = 27.
Assuming K will become exponentially easy with practice.
Taken the values p=7, q=11 and e=17.
then the value of n=p*q=77 and f(n)=(p-1)(q-1)=60.
Therefore, our public key pair is,(e,n)=(7,77)
Now for calvulating the value of d we have the constraint,
e*d == 1 mod (f(n)), [here "==" represents the **congruent symbol**].
17*d == 1 mod 60
(17*53)*d == 53 mod 60, [7*53=901, which gives modulus value 1]
1*d == 53 mod 60
hence,this gives the value of d=53.
Therefore our private key pair will be, (d,n)=(53,77).
Hope this help. Thank you!

Resources