Calculate possible equations that produce a given integer [closed] - vb.net

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 an integer of x and I am trying to find what equation is possible to produce that integer. This is for the purpose of script obfuscation. I have solved this problem for addition, subtraction, division, square roots, xor, and hex. Most of my answers involve using an inverse equation to solve. I am looking to solve it for:
Multiplication
Modulus
Exponents
Scientific Notation
Logarithm
Any others I did not mention
Here is the Function that I started:
Public Function IntegerBreak(PickedInteger)
Randomize()
Dim Switch, Entropy, Result As Integer
Switch = Int(6 * Rnd()) + 1
Entropy = Int(100 * Rnd()) + 1
Select Case Switch
Case 1
'addition
Result = PickedInteger - Entropy
Return Result & " + " & Entropy & " = " & PickedInteger
Case 2
'subtraction
Result = PickedInteger + Entropy
Return Result & " - " & Entropy & " = " & PickedInteger
Case 3
'division
Result = PickedInteger * Entropy
Return Result & " / " & Entropy & " = " & PickedInteger
Case 4
'squares
Result = PickedInteger ^ 2
Return "Sqr (" & Result & ") = " & PickedInteger
Case 5
'xor
Result = PickedInteger Xor Entropy
Return Result & " Xor " & Entropy & " = " & PickedInteger
Case 6
'hex
Return "&H" & Conversion.Hex(PickedInteger) & " = " & PickedInteger
End Select
End Function
Sample Output:
-47 + 57 = 10
89 - 79 = 10
260 / 26 = 10
Sqr(100) = 10
12 Xor 6 = 10
&HA = 10
EDIT: My specific question is how to find out:
(z*y=x, z%y=x, z^y=x, zLogy=x, or 3.123^10=x)
What is stopping me from continuing is I do not have a solid background in mathematics, I ran into issues for multiplication because there can be multiple answers on how you calculate an equation:
(2*6=12, 3*4=12)
Using the inverse method gave me decimal numbers. I would like any advice for calculating Multiplication, Modulus, Exponents, Scientific Notation, and Logarithms.
Also what is some good information for learning obfuscation?
Answers are acceptable in vb.net or vbscript or mathematical pseudocode.
Pointing me in the right direction would be appreciated.

My specific question is how to find out:
(z*y=x, z%y=x, z^y=x, zLogy=x, or 3.123^10=x)
For multiplication, divide both sides by y and you get:
z = x / y
Note that even if x and y are both integers, you may get a fractional z (eg. 2 / 7).
For modulus, note that the result of z % y cannot be greater than or equal to y. So choosing an arbitrary y probably won't be useful, since depending on the value chosen you may not be able to reach x at all. You could do it like this:
z = x + rnd()*y
as long as you choose a y value greater than x. Note that this definitely only works for integers.
For power, if x ^ y = z then:
x = z ^ (1/y)
(Solving y ^ x = z for x is completely different.)
Your logarithm example doesn't make a lot of sense because log is a unary operation, which means it only operates on one number instead of two. Unless you actually mean "log base y", in which case for log{y} x = z:
x = y ^ z

Related

Where the Hamming Distance Constants Came From

The function:
function popcount (x, n) {
if (n !== undefined) {
x &= (1 << n) - 1
}
x -= x >> 1 & 0x55555555
x = (x & 0x33333333) + (x >> 2 & 0x33333333)
x = x + (x >> 4) & 0x0f0f0f0f
x += x >> 8
x += x >> 16
return x & 0x7f
}
Is for calculating Hamming Weight. I am wondering where these constants come from and generally how this method was discovered. Wondering if anyone knows the resource that describes it.
There masks select the even numbered k-bit parts, k=1 gives 0x55555555, k=2 gives 0x33333333, k=4 gives 0x0f0f0f0f.
In binary the masks look like:
0x55555555 = 01010101010101010101010101010101
0x33333333 = 00110011001100110011001100110011
0x0f0f0f0f = 00001111000011110000111100001111
They are also the result of 0xffffffff / 3, 0xffffffff / 5 and 0xffffffff / 17 but this arithmetic insight is probably not useful in this context.
Overall this method of computing the Hamming weight has the form of a tree where first adjacent bits are summed into a 2-bit number, then adjacent 2-bit numbers are summed into 4-bit numbers, and so on.
All the steps could have this form:
x = (x & m[k]) + ((x >> k) & m[k])
where m[k] is a mask selecting the even-numbered k-bit parts.
But many steps have short-cuts available for them. For example, to sum adjacent bits, there are only 4 cases to consider:
00 -> 00
01 -> 01
10 -> 01
11 -> 10
This could be done by extracting both bits and summing them, but x -= x >> 1 & 0x55555555 also works. This subtracts the top bit from the 2-bit part, so
00 -> 00 - 0 = 00
01 -> 01 - 0 = 01
10 -> 10 - 1 = 01
11 -> 11 - 1 = 10
Maybe this could be discovered through "cleverness and insight", whatever those are.
In the step x = (x + (x >> 4)) & 0x0f0f0f0f (extra parentheses added for clarity), a couple of properties are used. The results from the previous steps are the Hamming weights of 4-bit strings stored in 4 bits each, so they are at most 0100. That means two of them can be added in-place without carrying into the next higher part, because their sum will be at most 1000 which still fits. So instead of masking twice before the sum, it is enough to mask once after the sum, this mask effectively zero-extends the even numbered 4-bit parts into 8-bit parts. This could be discovered by considering the maximum values at each step.
The step x += x >> 8 has similar reasoning but it works out even better, even masking after the sum is not needed, this leaves some "stray bits" in the second byte from the bottom and in the top byte, but that is not damaging to the next step: the >> 16 throws away the second byte from the bottom, in the end all the stray bits are removed with x & 0x7f.

Using factorials to find combinations

So, this is a two part question but based on the same project. I am trying to write a small program that can illustrate how a computer can quickly crack a password, using a brute force attack. It only has three inputs: A check box to denote if it should use integers, a check box to denote if it should use letters, and a textbox to enter the password to be cracked. It then outputs the number of combinations. Here is my code:
dim a,b,c,d,P as double
'Using the following formula:
'P(n,r) = n!/(r!(n-r)!)
'Let's assume we are just using numbers, so n = 10
'r = the count of characters in the textbox.
a = factorial(n)
b = factorial(r)
c = (n - r)
d = factorial(c)
P = a / (b * d)
Output = "With a password of " & r & " characters and " & n & " possible values, the number of combinations are " & P
Me.RichTextBox1.Text = Output & vbCrLf
Function factorial(ByVal n As Integer) As Integer
If n <= 1 Then
Return 1
Else
Return factorial(n - 1) * n
End If
End Function
So, let's assume I'm only looking at the characters 0-9, with the following number of characters in a password, I get:
P(10,1) = 10!/(1! * (10-1)!) = 10
P(10,2) = 10!/(2! * (10-2)!) = 45
P(10,3) = 10!/(3! * (10-3)!) = 120
P(10,4) = 10!/(4! * (10-4)!) = 210
P(10,5) = 10!/(5! * (10-5)!) = 252
P(10,6) = 10!/(6! * (10-6)!) = 210
P(10,7) = 10!/(6! * (10-7)!) = 120
You can see the number of combinations goes down, once it gets past 5. I assume this is right, but wanted to check before I present this. Is this because the total number in the pool remains the same, while the sample increases?
My second question is about how to consider a password to crack that repeats numbers. Again, let's assume that we are just pulling from digits 0-9. If the sample size it two (lets say 15), then there are 45 possible combinations, right? But, what if they put in 55? Are there still 45 combinations? I suppose the computer still needs to iterate over every possible combination, so it would still be considered 45 possibilities?

Basic Gauss Elimination yields wrong result? [closed]

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 9 years ago.
Improve this question
I was reading a book and got stuck at a particular point. I stuck here in the following and want to know that how x+(y+z)=0.0 calculated ?
Further in the following example of Gauss elimination method I couldn't get how 2.5/(-0.005)=-2500 got calculated? From where they took this "-0.005" value .
Computers do not do arithmetic in the same way that people do on pen and paper. Numbers have limited precision. Imagine you had a number system where you could only have 4 digits after the decimal point and also a factor of 10 to some power, and so numbers looked like:
±0._ _ _ _ × 10ⁿ
Now, add these two numbers:
0.1234 × 10⁸
0.5678 × 10⁰
You are adding
12340000
and
00000000.5678
The real sum is
12340000.5678,
but the theoretical computer here can store only the first four digits, giving
12340000 = 0.1234 × 10⁸
That is why y+z in the textbook problem is equal to y, and x + (y + z) = 0 ≠ (x + y) + z.
x = 0.1 × 10¹⁰
y = -0.1 × 10¹⁰
z = 0.1 × 10¹
x + y = 0.0
(x + y) + z = 0.0 + 0.1 × 10¹ = 0.1 × 10¹
(Single-precision) floats have only 8 digits of precision in IEEE arithmetic. These correspond to the C float datatype. But y = - 10⁹ z, and z disappears when you add y and z. So,
y + z = -999999999 = -0.1 × 10¹⁰ after rounding.
x + ( y + z) = 0.0
The book also has a typographical error. The quotient should have been 2.5/(-0.001), from rows 2 and 3, column 2 of the matrix.
This is why computer algorithms for matrix algebra are tricky; they seek to minimize the effect of roundoff error and underflow. Unfortunately, any flaw in an algorithm can lead to very bad problems. One test is to look at the Hilbert matrix
H_n = (1/(i+j-1)) 1 ≤ i, j ≤ n
The inverse of this matrix is has integer entries, but that matrix and its inverse are spectacularly ill-conditioned. Any numerical error in computing the inverse will lead to wildly-wrong values. Twenty years ago, I tested the inverse routine for the then-current version of Matlab. It was acceptable for H_10, but too poor to use for H_12.

I need some help on designing a program that will perform a minimization using VBA Excel

How do I use Excel VBA to find the minimum value of an equation?
For example, if I have the equation y = 2x^2 + 14, and I want to make a loop that will slowly increase/decrease the value of x until it can find the smallest value possible for y, and then let me know what the corresponding value of x is, how would I go about doing that?
Is there a method that would work for much more complicated equations?
Thank you for your help!
Edit: more details
I'm trying to design a program that will find a certain constant needed to graph a nuclear decay. This constant is a part of an equation that gets me a calculated decay. I'm comparing this calculated decay against a measured decay. However, the constant changes very slightly as the decay happens, which means I have to use something called a residual-square to find the best constant to use that will fit the entire decay best to make my calculated decay as accurate as possible.
It works by doing (Measured Decay - Calculated Decay) ^2
You do that for the decay at several times, and add them all up. What I need my program to do is to slowly increase and decrease this constant until I can find a minimum value for the value I get when I add up the residual-squared results for all the times using this decay. The residual-squared that has the smallest value has the value of the constant that I want.
I already drafted a program that does all the calculations and such. I'm just not sure how to find this minimum value. I'm sure if a method works for something like y = x^2 + 1, I can adapt it to work for my needs.
Test the output while looping to look for the smallest output result.
Here's an Example:
Sub FormulaLoop()
Dim x As Double
Dim y As Double
Dim yBest As Double
x = 1
y = (x ^ 2) + 14
yBest = y
For x = 2 To 100
y = (x ^ 2) + 14
If y < yBest Then
yBest = y
End If
Next x
MsgBox "The smallest output of y was: " & yBest
End Sub
If you want to loop through all the possibilities of two variables that make up x then I'd recommend looping in this format:
Sub FormulaLoop_v2()
Dim MeasuredDecay As Double
Dim CalculatedDecay As Double
Dim y As Double
Dim yBest As Double
MeasuredDecay = 1
CalculatedDecay = 1
y = ((MeasuredDecay - CalculatedDecay) ^ 2) + 14
yBest = y
For MeasuredDecay = 2 To 100
For CalculatedDecay = 2 To 100
y = ((MeasuredDecay - CalculatedDecay) ^ 2) + 14
If y < yBest Then
yBest = y
End If
Next CalculatedDecay
Next MeasuredDecay
MsgBox "The smallest output of y was: " & yBest
End Sub

Division in VB.NET

What's the difference between / and \ for division in VB.NET?
My code gives very different answers depending on which I use. I've seen both before, but I never knew the difference.
There are two ways to divide numbers. The fast way and the slow way. A lot of compilers try to trick you into doing it the fast way. C# is one of them, try this:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine(1 / 2);
Console.ReadLine();
}
}
Output: 0
Are you happy with that outcome? It is technically correct, documented behavior when the left side and the right side of the expression are integers. That does a fast integer division. The IDIV instruction on the processor, instead of the (infamous) FDIV instruction. Also entirely consistent with the way all curly brace languages work. But definitely a major source of "wtf happened" questions at SO. To get the happy outcome you would have to do something like this:
Console.WriteLine(1.0 / 2);
Output: 0.5
The left side is now a double, forcing a floating point division. With the kind of result your calculator shows. Other ways to invoke FDIV is by making the right-side a floating point number or by explicitly casting one of the operands to (double).
VB.NET doesn't work that way, the / operator is always a floating point division, irrespective of the types. Sometimes you really do want an integer division. That's what \ does.
10 / 3 = 3.333
10 \ 3 = 3 (the remainder is ignored)
/ Division
\ Integer Division
10 / 3 = 3.33333333333333, assigned to integer = 3
10 \ 3 = 3, assigned to integer = 3
20 / 3 = 6.66666666666667, assigned to integer = 7
20 \ 3 = 6, assigned to integer = 6
Code for the above:
Dim a, b, c, d As Integer
a = 10 / 3
b = 10 \ 3
c = 20 / 3
d = 20 \ 3
Debug.WriteLine("10 / 3 = " & 10 / 3 & ", assigned to integer = " & a)
Debug.WriteLine("10 \ 3 = " & 10 \ 3 & ", assigned to integer = " & b)
Debug.WriteLine("20 / 3 = " & 20 / 3 & ", assigned to integer = " & c)
Debug.WriteLine("20 \ 3 = " & 20 \ 3 & ", assigned to integer = " & d)