# Tagged Questions

Programming questions that are significantly affected or best defined by the underlying mathematics of the problem.

326 views

### Which if statement requires less computation?

I have if (!b && c || a && c) //do action a else //... Due to some internal relationship between a, b, and c. !b && c || a && c is proved to be equivalent to (! ...
4k views

### Why does division and multiplication by 2 use the shift operator rather than division operator?

While looking at the code in C for division/multiplication by 2, it is found that shift operator is used rather than division operator. What is the advantage of using shift over division operator?
457 views

### Why is float the default in the majority of languages?

In programming languages, for represent numbers you have (mainly) two types: Int(s) and floats. The us of Int is very easy. However floats cause a lot of unexpected surprises: http://anh.cs.luc.edu/...
571 views

### How fast should a Python factoring script be?

Just how efficient is "good enough" for all intents and purposes? I wrote a script to simply list off all numbers that divide into an input, x, as pairs (i, n//i) and was just curious how efficient I ...
4k views

### When comparing floats, what do you call the threshold of difference?

I'm comparing floats in Java right now and the simplest formula is: Math.abs(a - b) < THRESHOLD When naming your variable for the threshold of difference, should you name it delta or epsilon? ...
1k views

### Why is mod (%) a fundamental mathematical operator in many programming languages?

Is there a reason, historical or otherwise, why the modulus operator is part of a small set of standard operators in what seems like many languages? (+, -, *, / and %, for Java and C, with ** in Ruby ...
5k views

### Web Developer and Math [duplicate]

I have just learned HTML/CSS and I practice everything I read to make it more understandable to me and I really enjoy it. My goal is to be a Modern Web Developer both front and back-end, so I won't ...
308 views

I have a problem I am developing a solution for and currently I solve it with a brute force solution that checks all possibilities. It works for small numbers of bins but I'd like to work with a ...
605 views

### Trying to understand the 2N lnN compares for quicksort

I was going through the analysis of quicksort in Sedgewick's Algorithms book. He creates the following recurrence relation for number of compares in quicksort while sorting an array of N distinct ...
935 views

### Sum of divisors of numbers of the range ~ 10^6

I was trying to find the sum of divisors of numbers upto 106. The test cases are like of the order of 105. I have done some pre processing like int divisors(int num) { int sum=0; for(int i=1;...
548 views

### Using Power of 2 numbers to represent types

Let's say that we have some values, represented by power of 2: TYPE_1 = 1 TYPE_2 = 2 TYPE_3 = 4 TYPE_4 = 8 ... I need to store some of these types in one value. Example: To represent TYPE_1 with ...
4k views

### How to get from 1 byte to 2 bytes

We have 1 byte, which is 8 bits, which is 2^8. Now 2 bytes should be 2 * 1 byte, which is 2 * 2^8 = 2^9, but actually 2 bytes is 2^16. What am I missing here? It seems like 2 bytes isn't 2 * 1 byte, ...
664 views

### Python — Time complexity of built-in functions versus manually-built functions in finite fields

Generally, I'm wondering about the advantages versus disadvantages of using the built-in arithmetic functions versus rolling your own in Python. Specifically, I'm taking in GF(2) finite field ...
2k views

### Documenting mathematical logic in code

Sometimes, although not often, I have to include math logic in my code. The concepts used are mostly very simple, but the resulting code is not - a lot of variables with unclear purpose, and some ...
3k views

### Array Multiplication and Division

I came across a question that (eventually) landed me wondering about array arithmetic. I'm thinking specifically in Ruby, but I think the concepts are language independent. So, addition and ...
1k views

### Does learning to play an instrument improve programming ability? [closed]

I've seen plenty of questions asking if listening to music boosts productivity, etc. but I haven't been able to find one about performing music. Learning to play the piano has been on my to-do list ...
268 views

### Finding maximum number of congruent numbers

Let's say we have a multiset (set with possible duplicates) of integers. We would like to find the size of the largest subset of the multiset such that all numbers in the subset are congruent to each ...
280 views

### Optimization algorithm, how to develop this problem

I have a situation where I would like your input and think over / solve algorithm. The specs There are inventories with a specified amount of capacity. 'X (width) and Y (time)' creates ...
543 views

### What is the difference between a combinator and function chaining?

So from what I've read about combinators I can't quite tell how they're different from simply chaining function calls. I know I must be missing something but I'm not figuring out what I'm missing. I ...
320 views

### Pairwise testing, not possible to say which combinations is faulty?

Let's say I have 4 (A,B,C,D) parameters with 3 possible values, also 81 unique combinations. With e.g. orthogonal array, I will end up with 9 test cases, each combining 3 pairs. But that means that if ...
110 views

### Find how the data has been processed

Well this has been a question I have puzzled about for a long time: Suppose I have a dictionary of input:output like: This {2: 6, 5: 15, 20: 60, 26: 78, ... 982: 2946, 997: 2991} where ...
881 views

### Transform math formula into code (line-line intersection)

I'm having hard times transforming a math formula to code. I can solve it on the paper easily, but it's hard for me to bring it into code form. Is it generally possible to bring a math formula ...
2k views

### Is there such a thing as truly random? [closed]

I saw a video about random numbers and how the programmer in that video was talking about computers generating pseudo random numbers and that they are not really random. I knew about this. Then he ...
443 views

### algorithm to create all possible column vectors of size N containing only ones and zeroes [closed]

I am a mathematics student and am just starting to learn about programming. I am stuck on a particular program I am trying to write for fun. I want to find an algorithm to create all possible column ...
7k views

### Closest Point of Approach (CPA) mathematical formula in ship radar

I was recently searching for the mathematical formula to find closest point of approach (CPA) between one ship and another ship. I need to apply the formula in my radar ship program and I can't find ...
294 views

### Customer Requirements Contains Equations that Cancel to Nothing

I have a project where the customer requirement specifies a report and contains mathematical equations for the contents of some of the columns on that report. One of the columns on this report is a ...
890 views

### Is (1/(1/x)) always a perfect round trip?

Is the following guaranteed to return true for all numerical and non-zero values of x? bool IsRoundTrip(double x) { double y = 1 / (1 / x); return x == y; } What conditions would cause a ...
227 views

### Tracking scientific error when working with floating-point numbers

Background I hate the way .Net/IEEE-754 handles equality of floating-point numbers (FPNs) (i.e. double, float). It requires the programmer to be prescient with respect to the yet-to-be-determined ...
12k views

### what is a efficient way to find repeating decimal

I am trying to find a efficient algorithm in Java to find the repeating decimal part of two integers a and b where a/b. eg. 5/7 = 0.714258 714258.... I currently only know of the long division ...
391 views

### strategies for dealing with machine epsilon

Say you have a situation where you divide and then multiply a float, and you need to guarantee that it survives macheps (ie multiplication output equals division input). What are known strategies for ...
324 views

### I'm trying to create a visual representation of something, but I don't know what words to use, so I'll try to describe it

My project team is making a site that will use reddit style voting to track users' opinions on various issues, and use the data to create a "heat map". I say heat map in quotes because I'm not sure ...
146 views

### Randomness of wrapped RNG

There are plenty of questions around regarding random number generation, but I couldn't find anything that exactly matched my question. Apologies if I missed one. Most random number generators in ...
583 views

### Algorithm to detect a CLICK within Square range

It might be a simple question but I am looking for optimal solution. I will have numbers printed on a screen and I will be aware of coordinates. Numbers/Symbols will have 4 points(Square) to define ...
879 views

### Interview puzzle on traveling on a line segment

On a number line of length M, where 0 < M <= 1,000,000,000, you given N (1 < N <= 100,000) integer pairs of points. In each pair, the first point represents where an object is currently ...
119 views

### Turning n-dim points into m-dim where m<n and where point-to-point distance deviation is minimized

I want to apply k-means clustering to a (sparse) adjacency graph. For this I need to assign the nodes to a position in an euclidean space. Trivially I can do this by having a space with as many ...
168 views

### How to do scalar multiplication and matrix inverse when variables are of size 1000 bits?

I am doing arithmetic operations on really huge numbers. For example, I am given six variables, a_{11}, a_{12}, a_{21}, a_{22}, x_1, and x_2. Although the above are math terms, these six variables ...
2k views

### Randomly Generate Points in a Spiral Motion using Javascript

So I have a javascript program that currently gets random points, it generates a random x,y,z value around the center point, within the radius r var maxArms = 3; var i = 0; var color = []; ...
5k views

### How to get audio spectrum analysis? [closed]

I need to find or create a tool that analyzes the audio spectrum of a sound file (like a .wav or .mp3). I need to output the "volume" or power of x number of frequency bands and output the data as ...
6k views

### Find the new coordinates using a starting point, a distance, and an angle

Okay, say I have a point coordinate. var coordinate = { x: 10, y: 20 }; Now I also have a distance and an angle. var distance = 20; var angle = 72; The problem I am trying to solve is, if I want ...
1k views

### Date calculation algorithm

I'm working on a project to schedule a machine shop, basically I've got everything covered BUT date calculations, I've got a method called schedule (working on PHP here): public function schedule(\$...
902 views

### Separating merged array of arithmetic and geometric series [closed]

Given an array of positive integers in increasing order. Separate them in two series, an arithmetic sequence and geometric sequence. The given array is such that a solution do exist. The union of ...
3k views

### Calculating 3d rotation around random axis

This is actually a solved problem, but I want to understand why my original method didn't work (hoping someone with more knowledge can explain). (Keep in mind, I've not very experienced in 3d ...
17k views

### How to check if 4 points form a square?

Assume I have 4 points (they are 2-dimension), which are different from each other, and I want to know whether they form a square. How to do it? (let the process be as simple as possible.)
964 views

### how to programtically build a grid of interlocking but random sized squares

I want to create a two dimensional layout of rectangular shapes, a grid made up of random sized cubes. The cubed should fit together and have equal padding or margin (space between). Kind of like a ...
3k views

### Can you use Pi as a crude random number generator?

I recently saw this question over at math.SE. It got me thinking. Could Pi be used as a crude random number generator? I mean the results are well known(how long has pi been computed to now?) but, Pi ...
5k views

### Why use other number bases when programming

My coworkers and I have been bending our minds to figuring out why anyone would go out of their way to program numbers in a base other than base 10. I suggested that perhaps you could optimize longer ...
459 views

### Should Equality be commutative within a Class Hierachy?

It is easy to define the Equals operation in ways that are not commutative. When providing equality against other types, there are obviously situations (in most languages) were equality not being ...
350 views

### What's the normal way machine-learning algorithms are integrated into normal programs?

I'm currently taking a machine learning course for fun, and the course heavily focuses on Matlab/Octave to write the code. One thing mentioned in the course is that, while Matlab/Octave are great for ...