discrete-mathematics

Is it possible to implement bitwise operators using integer arithmetic?

淺唱寂寞╮ 提交于 2019-12-02 14:06:30
I am facing a rather peculiar problem. I am working on a compiler for an architecture that doesn't support bitwise operations. However, it handles signed 16-bit integer arithmetics and I was wondering if it would be possible to implement bitwise operations using only: Addition ( c = a + b ) Subtraction ( c = a - b ) Division ( c = a / b ) Multiplication ( c = a * b ) Modulus ( c = a % b ) Minimum ( c = min(a, b) ) Maximum ( c = max(a, b) ) Comparisons ( c = (a < b), c = (a == b), c = (a <= b), et.c. ) Jumps ( goto, for, et.c. ) The bitwise operations I want to be able to support are: Or ( c =

What number in binary can only be represented as an approximation?

北城以北 提交于 2019-12-01 14:11:38
In decimal (base 10), 1/3 can only be approximated to 0.33333 repeating. What number is the equivalent in binary that can only be represented as an approximation? Andrew Song 0.1 is one such example, as well as 0.2 This question is also similar to this other SO question , which already has very good answers. A better question is to ask what numbers can be represented exactly in binary. Everything else can only be approximated or not represented at all. See What every computer scientist should know about floating point arithmetic . Well, there are infinite numbers that can't be precisely

Maximum value of postage stamps on an envelope

ぐ巨炮叔叔 提交于 2019-12-01 03:35:05
The postage stamp problem is a mathematical riddle that asks what is the smallest postage value which cannot be placed on an envelope, if the letter can hold only a limited number of stamps, and these may only have certain specified face values. For example, suppose the envelope can hold only three stamps, and the available stamp values are 1 cent, 2 cents, 5 cents, and 20 cents. Then the solution is 13 cents; since any smaller value can be obtained with at most three stamps (e.g. 4 = 2 + 2, 8 = 5 + 2 + 1, etc.), but to get 13 cents one must use at least four stamps. Is there an algorithm that

Maximum value of postage stamps on an envelope

旧巷老猫 提交于 2019-12-01 00:14:17
问题 The postage stamp problem is a mathematical riddle that asks what is the smallest postage value which cannot be placed on an envelope, if the letter can hold only a limited number of stamps, and these may only have certain specified face values. For example, suppose the envelope can hold only three stamps, and the available stamp values are 1 cent, 2 cents, 5 cents, and 20 cents. Then the solution is 13 cents; since any smaller value can be obtained with at most three stamps (e.g. 4 = 2 + 2,

Number of n-element permutations with exactly k inversions

浪子不回头ぞ 提交于 2019-11-30 12:03:04
问题 I am trying to efficiently solve SPOJ Problem 64: Permutations. Let A = [a1,a2,...,an] be a permutation of integers 1,2,...,n. A pair of indices (i,j), 1<=i<=j<=n, is an inversion of the permutation A if ai>aj. We are given integers n>0 and k>=0. What is the number of n-element permutations containing exactly k inversions? For instance, the number of 4-element permutations with exactly 1 inversion equals 3. To make the given example easier to see, here are the three 4-element permutations

Find two missing numbers

时光总嘲笑我的痴心妄想 提交于 2019-11-30 05:10:17
have a machine with O(1) memory. we want to pass n number (one by one) first time, and again we exclude two numbers and we will pass n-2 of them to machine. write an algorithm that finds missing numbers. This was an interview question and I couldn't solve it. It can be done with O(1) memory. You only need a few integers to keep track of some running sums. The integers do not require log n bits (where n is the number of input integers), they only require 2b+1 bits, where b is the number of bits in an individual input integer. When you first read the stream add all the numbers and all of their

Number of n-element permutations with exactly k inversions

别来无恙 提交于 2019-11-30 01:57:49
I am trying to efficiently solve SPOJ Problem 64: Permutations . Let A = [a1,a2,...,an] be a permutation of integers 1,2,...,n. A pair of indices (i,j), 1<=i<=j<=n, is an inversion of the permutation A if ai>aj. We are given integers n>0 and k>=0. What is the number of n-element permutations containing exactly k inversions? For instance, the number of 4-element permutations with exactly 1 inversion equals 3. To make the given example easier to see, here are the three 4-element permutations with exactly 1 inversion: (1, 2, 4, 3) (1, 3, 2, 4) (2, 1, 3, 4) In the first permutation, 4 > 3 and the

What does this definition of contiguous subsequences mean?

烈酒焚心 提交于 2019-11-29 17:10:45
问题 I don't understand the following definition of a contiguous subsequence: A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. If S is {5, 15, -30, 10, -5, 40, 10} then 15, -30, 10 is a contiguous subsequence. What makes 15, -30, 10 a contiguous subsequence? 回答1: The form a subset that are next to each other within the set. con·tig·u·ous/kənˈtigyo͞oəs/Adjective 1. Sharing a common border; touching. 2. Next or together in sequence. 回答2: A contiguous

Finding where plots may cross with octave / matlab

萝らか妹 提交于 2019-11-29 11:30:51
I have several data points that are plotted below and I would like to find the frequency value when the amplitude value crosses 4 . I've included an example along with the data points in the example below. I've circled the answer graphically but I'm not sure how to compute it mathematically and get all the values for the frequencies I desire. How can I do this with octave / matlab? Also is there a mathematical term for what I'm trying to do? In this example I'm trying to get 5 frequencies (but this is just an example) I know two answers are 30 and 80 but not sure how to get the rest. The full

Finding a Eulerian Tour

北战南征 提交于 2019-11-29 07:08:56
I am trying to solve a problem on Udacity described as follows: # Find Eulerian Tour # # Write a function that takes in a graph # represented as a list of tuples # and return a list of nodes that # you would follow on an Eulerian Tour # # For example, if the input graph was # [(1, 2), (2, 3), (3, 1)] # A possible Eulerian tour would be [1, 2, 3, 1] I came up with the following solution, which, while not as elegant as some of the recursive algorithms, does seem to work within my test case. def find_eulerian_tour(graph): tour = [] start_vertex = graph[0][0] tour.append(start_vertex) while len