Code to calculate a certain arithmetic expression in assemlby [closed] - math

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Expression: z=a+b * b-(36/(b * b)/(1+(25/(b * b)))
I have no idea what data directives I should use and in what order I should write the code.

C = A + B for Z80 CPU:
ld a,A
ld b,B
add a,b ; a = C
C = A * B for 68000 CPU:
MOVE.W D0,A
MOVE.W D1,B
MULS.W D1,D0 ; D0 = C
et cetera ... check your target CPU instruction guide to see what arithmetic operations it does implement directly, and what operand types can be used for them, which registers you have available, and their data type...
Looks like you don't have to write universal math expression parser (this gets tricky quite quickly, once at high-school we had on programming competition the task to write exactly that, and at first we were like "what, a single task for 5h time, I will be done in 30min" ... then after 5h nobody's code passed the full test suite, best were around 80% correct).
So if only this particular expression should be calculated, you can "parse" it by hand, simplifying it down into particular steps involving only single operation and one of intermediate sub-results. Then just write that with your instructions, step by step, like you would calculate that by hand (also make sure you conform to the math expression calculation rules, you know which operations have priority over others? Parentheses override anything, then mul/div first, add/sub later, from left to right, but this is base school math stuff, so unless you are 10y.o., you shouldn't ask this).
If your CPU does not have instruction for division or multiplication, simply implement it by subtraction/addition in loop. It's very lame performance wise, but if you have to ask this question, then one can not expect you would even comprehend more advanced algorithm for these.

Related

Hash Function which allows for comparison of input without knowing input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to find functions F and F_Verification such that:
F(Input1, RandomSeed1) = Output1
F(Input1, RandomSeed2) = Output2
F_Verification (Output1, Output2) = TRUE
Cryptographic requirements:
Given Output1 and Output2, you must not be able to find input.
F may be a surjective function yielding the same output ~1/Billion~ times. It would be best to have some flexibility in how often it is surjective.
F_Verification May yeild a false positive again ~1/Billion~ times with some flexibility
Extra Details
1. All Inputs will be unique
F_Verification must be a quick function
Ideally F is a slow function
Any suggestions on starting places would be much appreciated.
Thank you.
In general, you're going to have a hard time finding a function that does this, because the output of most cryptographic functions is designed to be indistinguishable from random. Therefore, it's unlikely that you're going to find a function that both produces two identical outputs based on a random seed and lets you recover any information about the input value without knowing the seed.
However, there are some constructions that you can use. If the two outputs can be the exact same output, then you can just skip the random seed and let F be a secure hash function, like SHA-256 or BLAKE2b. Then the verification is an equality comparison. This is the ideal situation that hash functions are designed for, and it's what I recommend unless you know you need different behavior.
If you need to have two different outputs, you can make F this:
F(input, seed) = SHA-256(input) || HMAC-SHA-256(seed, input)
This still reveals that your objects are the same, but it provides different output on a different seed. The verification is an equality comparison of the first 32 bytes.
Beyond that, you're looking at situations where your verification isn't cheap. You can use RSA-PSS to sign the objects, and the salt you use in the signature will make the two signatures different even if the input (which you will have then hashed) is the same. But RSA verification isn't super cheap as far as cryptographic functions go, although there are worse options.
Pretty much all the other operations involve knowing the seed in order to compare, and you haven't provided that as an option. If it is, you could use a 256-bit seed and make F into this:
F(input, seed) = AES-Key-Wrap(seed, SHA-256(input))

If a non-deterministic Turing machine runs in f(n) space, then why does it run in 2^O(f(n)) time? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
Assuming that f(n) >= n.
If possible, I'd like a proof in terms of Turing machines. I understand the reason why with machines that run on binary, because each "tape cell" is a bit with either 0 or 1, but in Turing machines a tape cell could hold any number of symbols. I'm having trouble why the base is '2' and not something like 'b' where 'b' is the number of types of symbols of the Turing machine tape.
The important detail here is that the runtime is 2O(n) rather than O(2n). In other words, the runtime is "two raised to the power of something that's O(n)," rather than "something that's on the order of 2n." That's a subtle distinction, so let's dissect it a bit.
Let's consider the function 4n. This function is not O(2n), because 4n outgrows 2n in the long run. However, notice that 4n = 22n, and since 2n = O(n) we can say that 4n = 2O(n).
Similarly, take bn for any base b. If b > 2, then bn is not O(2n). However, we do have that
bn = 2(lg b) n = 2O(n)
because (lg b) n = O(n), since (lg b) is just a constant factor.
It is definitely a bit weird that O(2n) is not the same 2O(n). The idea of using big-O notation in exponents is somewhat odd the first time you see it (for example, nO(1) means "something bounded by a polynomial"), but you'll get used to it with practice.

Choosing the appropriate coding expression for a given job [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Common Lisp provides many flexible coding options for achieving a given result. However, it is sometimes difficult to choose the best approach. For example, the following vector expressions all produce the same result in different ways.
(defparameter objects (list 1 2 3))
(apply #'vector objects)
(coerce objects 'vector)
(make-array (length objects) :initial-contents objects)
(read-from-string (format nil "#~S" objects))
Of course, some expressions are more flexible than others, depending on the required output; but for a given output as above, what criteria are useful for deciding which to use?
(apply #'vector objects) is subject to the usual limitations of APPLY, which is that objects shouldn't hold more than CALL-ARGUMENTS-LIMIT elements. This is bad style even when you have only a few arguments.
COERCE is great: not only it performs the job, it also conveys the intent very well. However, you won't be able to give additional parameters for the resulting vector (e.g. fill-pointer, etc.); you cannot convert nested lists into matrices.
MAKE-ARRAY gives you full control over the resulting array: adjustability, fill-pointer, dimensions, element-type, displacement.
READ-FROM-STRING is a big no for data conversion, in general. In terms of useless computations, this approach is the Rube Goldberg's version of coerce. It also comes with a lot of security concerns, unless you are 100% sure about what the string contains. Here, you create the string yourself, but if your data contains any value for which another part of the code redefined PRINT-OBJECT, the code might break.

Is there any way to do example based programming? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to automatically generate a program/function by writing a certain amount of examples which show the before and after? How many examples would be needed to insure correctness and lack of holes? What is the name of doing such an automatic process?
It's called program synthesis or perhaps guessing. Your question reminds me of the 2013 ICFP programming contest where you were supposed to guess programs and all you had was the output for your chosen test input in a very simple functional language.
So imagine you wanted to make the identity function:
(define-by-example identity
(0 -> 0)
(1 -> 1))
There is something missing? What if I told you to make a function that returns it's own value for 0 and 1 only? How would you make that different than the above example? What if it's a non linear transformation. Eg. how many examples does it take to get a polynomial correct?
I guess we're back to something like this:
(define-by-example fib
(0 -> 0)
(1 -> 1)
(n -> (+ (fib (- n 1)) (fib (- n 2)))))
But many languages has exactly this. Even Schemes dirty cousin Racket has this:
#!racket
(define (fib n)
(match n
[0 0]
[1 1]
[n (+ (fib (- n 1)) (fib (- n 2)))]))
This is the Hello World of Haskell so we better write that next :-)
-- haskell
fib 0 = 0
fib 1 = 1
fib n = fib(n - 1) + fib(n - 2)
Now there are smart people out there that tries to find the best method of transferring the idea down to a syntax that is clear for the computer and us. I'm sure the essence of "by example" is good but it needs to be a way of explaining the corrolations too and I cannot think of a better way to do that than maths or code where maths don't apply.
This is a subdomain of machine learning called supervised learning. The most popular method to solve these problems is by using neural networks. Another method is genetic programming.
In the general case it is not possible to guarantee correctness, no matter how large the training set (number of examples) is. But in a typical application this is not required. The training is considered successfull if a high enough percentage os the results is correct.
Is it possible to automatically generate a program/function by writing a certain amount of examples which show the before and after?
I'd say yes, but ...
... it's not exactly that easy, and certainly not that generally applicable. There are basically two "approaches" that I know of:
a) Try to generate a program
Just pack together program code (most probably instructions from some instruction set) until you get the desired results. This can be done with brute force, but it's hardly possible to synthesize non-trivial functions, yet alone complete programs, or probably with techniques from your favorite artificial intelligence tool set, like hill climbing, simulated annealing, genetic programming and the whole rest.
The only reference that I know of is the "Superoptimizer" by Henry Massalin, which tries to generate a function that computes the same as a given function, just with fewer instructions.
It's probably better to use some higher level representation of "computation" than assembler instructions (probably the Lisp AST?), but that's just my guess.
b) Write a meta program that "somehow" learns to behave like the desired program
This is actually very common nowadays in the form of neural networks, e.g. for image or voice recognition. Here you don't try to get a program that does what you want (e.g. "recognize birds in arbitrary images") but rather write a general program that is capable of learning to "behave differently" and train that in order to behave as you want.
Note that there's a lot of effort going into understanding what these meta programs actually do after they learned their intended function. Just taking the first relevant result from a Google search is already an interesting read.

How can determine dice sum probabilities? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In trying to solve a particular Project Euler question, I ran into difficulties with a particular mathematical formula. According to this web page (http://www.mathpages.com/home/kmath093.htm), the formula for determining the probability for rolling a sum, T, on a number of dice, n, each with number of sides, s, each numbered 1 to s, can be given as follows:
alt text http://www.freeimagehosting.net/uploads/8294d47194.gif
After I started getting nonsensical answers in my program, I started stepping through, and tried this for some specific values. In particular, I decided to try the formula for a sum T=20, for n=9 dice, each with s=4 sides. As the sum of 9 4-sided dice should give a bell-like curve of results, ranging from 4 to 36, a sum of 20 seems like it should be fairly (relatively speaking) likely. Dropping the values into the formula, I got:
alt text http://www.freeimagehosting.net/uploads/8e7b339e32.gif
Since j runs from 0 to 7, we must add over all j...but for most of these values, the result is 0, because at least one the choose formulae results are 0. The only values for j that seem to return non-0 results are 3 and 4. Dropping 3 and 4 into this formula, I got
alt text http://www.freeimagehosting.net/uploads/490f943fa5.gif
Which, when simplified, seemed to go to:
alt text http://www.freeimagehosting.net/uploads/603ca84541.gif
which eventually simplifies down to ~30.75. Now, as a probability, of course, 30.75 is way off...the probability must be between 0 and 1, so something has gone terribly wrong. But I'm not clear what it is.
Could I misunderstanding the formula? Very possible, though I'm not clear at all where the breakdown would be occuring. Could it be transcribed wrong on the web page? Also possible, but I've found it difficult to find another version of it online to check it against. Could I be just making a silly math error? Also possible...though my program comes up with a similar value, so I think it's more likely that I'm misunderstanding something.
Any hints?
(I would post this on MathOverflow.com, but I don't think it even comes close to being the kind of "postgraduate-level" mathematics that is required to survive there.)
Also: I definitely do not want the answer to the Project Euler question, and I suspect that other people that my stumble across this would feel the same way. I'm just trying to figure out where my math skills are breaking down.
According to mathworld (formula 9 is the relevant one), the formula from your source is wrong.
The correct formula is supposed to be n choose j, not n choose T. That'll really reduce the size of the values within the summation.
The mathworld formula uses k instead of j and p instead of T:
Take a look at article in wikipedia - Dice.
The formula here looks almost similar, but have one difference. I think it will solve your problem.
I'm going to have to show my ignorance here.... Isn't 9 choose 20 = 0? More generally, isn't n choose T going to always be 0 since T>=n? Perhaps I'm reading this formula incorrectly (I'm not a math expert), but looking at de Moive's work, I'm not sure how this formula was derived; it seems slightly off. You might try working up from Moive's original math, page 39, in the lemma.

Resources