# Questions tagged [math]

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

265 questions
Filter by
Sorted by
Tagged with
559 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/...
718 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 ...
11k 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? ...
3k 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 ...
7k 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 ...
342 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 ...
871 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 ...
2k 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;...
741 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 ...
9k 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, ...
2k 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 ...
3k 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 ...
5k 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 ...
3k 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 ...
320 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 ...
313 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 ...
1k 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 ...
606 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 ...
136 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 ...
2k 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 ...
9k 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 ...
1k 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 ...
14k 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 ...
309 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 ...
1k 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 ...
266 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 ...
32k 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 ...
546 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 ...
355 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 ...
159 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 ...
1k 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 ...
950 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 ...
129 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 ...
184 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 ...
3k 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 = []; ...
7k 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 ...
11k 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 ...
2k 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(\$...
1k 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 ...
4k 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 ...
46k 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.)
2k 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 ...
5k 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 ...
7k 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 ...
979 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 ...
378 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 ...
137 views

### Working with vectors and transformations

I am going to write an graphical 2D application that allows user to create polygons and transform them through transformation such as rotation an so on. I was hoping someone can give pro and cons ...
4k views

### Algorithm to calculate trajectories from vector field

I have a two-dimensional vector field, i.e., for each point (x, y) I have a vector (u, v), whereas u and v are functions of x and y. This vector field canonically defines a set of trajectories, i.e. ...