# Questions tagged [mathematics]

For code that aims to solve problems in all branches of mathematics. (Please do NOT use this tag for incidental trivial use of arithmetic. A simple test to apply is: would an amateur or professional mathematician take an interest in the question?)

605 questions
Filter by
Sorted by
Tagged with
940 views

### Third and second degree polynomial equation solver in C++

I'm quite new to C++, so as a beginner's project I decided to create a program that can solve second degree polynomials and (some) cubics using this lengthy formula I had found online. I would love to ...
555 views

### Calculating decimal places of pi in Python

I have written this small program in Python to calculate decimal places of $\pi$. I tried to code it as readably as possible, but since this is a bit calculation heavy, it is reasonable to think ...
• 690
1 vote
64 views

I'm making a program that can add 2 terms that the user types. I think I've got identifying or evaluating the terms down. I used classes because I just recently learned it and I also heard it was good ...
78 views

### converting from byte arrays to strings of integers, and back

I have this code to go from an array of bytes (arbitrarily long) to a "bigint" string (really, an arbitrarily long string composed of integers 0-9), and go from that long integer string into ...
• 351
321 views

### 2D Discrete Fourier Transform using Fortran

I am solving the 2D Wave Equation using Fourier Transform. The Discrete Fourier Transform (and the inverse also) is done inside the kx-loop and ...
• 1,142
311 views

### Permutation function for a math library in C++

Continuing the code for combinatorics library, adding the definition of ⁿPᵣ. This is an extract from a larger codebase. Is there any way to limit the code of gcd()...
3k views

### Sine function calculated via look-up table and linear interpolation

I have been attempting to implement a function calculating values of the sine function. I know that there are several similar threads regarding this topic but my goal was to try to implement such ...
• 385
95 views

...
• 175
1 vote
35 views

...
• 175
110 views

...
• 175
814 views

### Get the Most frequent element in an array

I have been trying to optimize this: ...
1 vote
99 views

### Recursive power (mathematical operation) function in C

How to make a C recursive exponentiation (power) function better? I developed a pretty simple (yet decent, imo) power function for the sake of learning. I'm not quite trying to compete with ...
1 vote
99 views

### Applied Solution Based On Polya Enumeration Theorem

Problem A function solution(w, h, s) that takes 3 integers and returns the number of unique, non-equivalent configurations that can be found on a grid w blocks wide, h blocks tall and s possible ...
1 vote
55 views

### Find circles for f(z, c) = z ^ 2 + c of length 6 in rational numbers

I've found pretty simple problem on Numberphiles channel: suppose we have a recursive function  \begin{equation} \begin{split} f_0 &= Z ^ 2+ C \\ f_i &= f_{i-1}^2 + C \; | \; i = 1 \dots \...
• 1,729
265 views

### Mean difference calculator for any int combination

What it does Calculates the mean difference for a list of any combination of ints (I think). What I want to know The only other way I can think of doing this is using recursion. I’d like to know if ...
• 301
834 views

### Python code to find greatest common divisor of multiple numbers

Hmm, I know this has been implemented countless times, and Python 3.9.5 math standard library has a built-in gcd() method that ...
• 1,726
4k views

### A math engine with support for complex numbers, calculus, and fractions

I've been creating a math engine in C++. I have decided now is a good time to ask for feedback from the community. Right now the main "good" things it has is complex numbers, calculus and a ...
• 91
95 views

### Formula filler and calculator

So I made this script for my calculator, so that I can just write whatever formula I want and the calculator would just ask for the missing pieces to do the calculations. I made it so that it would be ...
53 views

### Runge Kutta 2 Python ODE fluid flow

I am trying to solve a set of differential equations in x,y,z, I have made simple kutta 2 code in python before but not with 3 variables. I am not sure if what i have done is an acceptable way to ...
138 views

### Implementation of the square root for real time application

For the real time control software I need square root calculation. I have heard that the sqrt function from the standard library isn't good choice due to the unpredictable number of iterations used ...
• 213
634 views

### C++ class to create and evaluate Chebyshev approximations of arbitrary functions

I recently needed to create a function to approximate a complex trigonometric function on an embedded system without a floating point unit and without a fast trigonometric library. So I pulled out my ...
• 64.9k
147 views

### Sum of fractions

Objective: Create a function to sum a list of fractions (represented as pairs) Rules Only Prelude functions allowed Notes I was debating if I should create a ...
• 165
1 vote
2k views

### Lo Shu Magic Square (Python)

I wrote a python program to find if a matrix is a magic square or not. It works, but I can't help feeling like I may have overcomplicated the solution. I have seen other implementations that were a ...
• 315
514 views

### Finding distance between vectors of matrices

So here is the problem: Given 2D numpy arrays 'a' and 'b' of sizes n×m and k×m respectively and one natural number 'p'. You need to find the distance(Euclidean) of the rows of the matrices 'a' and ...
173 views

### Implementation of the basic matrix operations for embedded application in C++

I have been developing a control software in C++ and for implementation of the control algorithms I need basic matrix operations like addition, subtraction, multiplication and multiplication by scalar....
• 385
2k views

### Implementation of the trigonometric functions for real time application

I have been developing a real time application in C and I need to calculate sine and cosine of a given angle in radians. In respect to the fact that the application is a real time application the ...
• 213
315 views

### Can I further optimize this solution for HackerRank's “Making Candies”?

My C++ solution for HackerRank's "Making Candies" problem, reproduced below, is as optimized as I can make it and appears similar to what others say are optimal solutions. However, six of ...
• 258
215 views

### Finding Mersenne primes

An attempt to find Mersenne primes using the Lucas-Lehmer primality test: ...
• 11
765 views

### Determine the roots of a Quadratic Equation

A code that determines the roots of a quadratic equation. Also, determine the following based on the discriminant b² – 4ac: If b² − 4ac is positive, display: “There are TWO REAL SOLUTIONS.” If b² − ...
• 43
253 views

### C++20 generic math vector implementation

I wrote a math vector implementation using expression templates to add a little boost to "chain operations" like: ...
62 views

### Collect distances from 1 point to all others in a graph

Problem: I have a weighted graph. I want to get distance from definite point to all other points in the graph.(and then get path to them) I used modified dijkstra algorithm So, here is code: ...
96 views

### Small Python math library

I have been writing a math library with more options than the standard builtin one, partially to make my life easier in the future, and partially just for practice. Here is what I have so far in the ...
• 192
181 views

### Algorithm Optimization -- Automatic Dimensionality of PCA

I have implemented (rather, edited the implementation of) a technique to automatically detect the optimal number of dimensions for PCA, based off of this paper. This was inspired by ...
• 203
1 vote
62 views

### Java Summation Program

I'm new to Java so I have given myself small projects to help myself learn the ropes of java. My project was to create a function that would summate an expression no matter how high the degree or ...
154 views

### Calculation of the Distance Matrix in the K-Means Algorithm in MATLAB

Purpose of the code : To assign the corresponding label of the centroids to the points which are close to it. Below is a graphical (2D) example. Variable X is a matrix, rows represent the points, ...
280 views

### Bancroft's method implementation

Background I've written an algorithm to solve the three-dimensional Time Difference of Arrival (TDoA) multi-lateration problem. That is, given the coordinates of n ...
• 285
388 views

### Calculate the centroid of a collection of complex numbers

In working on another problem, one component I needed was to calculate the centroid of a collection of complex objects. The well-known way to calculate this is to simply average the real and ...
• 64.9k
243 views

### I need help optimizing a function that returns all divisors

This is my code: export function all_divisors(num: number): number[] { return range(1, num+1).filter(i => num % i === 0).toArray(); } the ...
• 51
123 views

### Estimate 𝜋 using Machin's formula

Overview Below is a simple program to estimate pi to the full precision of the provided type. It uses Machin's formula, but this question is about coding style / choices. It compares the result to <...
• 2,465
1 vote
271 views

### PowerShell - Find Least Common Multiple of a series of numbers

I am a Windows power user, a system tweaker, my idol is Leanardo da Vinci, and now I want to be a hacker. I am extremely new to programming, but I really am a fast learner if I am interested in the ...
• 1,726
1 vote
507 views

### Coin Flip Streaks script

I am attempting to complete the coin flip streaks problem from automate the boring stuff with python. My code works fine but my only concern is the phrasing of the task. Does the question want us to ...
• 19
335 views

### Generate prime factorisations of factorial numbers

The code was written to generate the factorisation of numbers into primes in ascending order. E.g 3! ='2 * 3', 4!='2^3 * 3'. When I have huge numbers such as 100!, the run time becomes a problem. I ...
1 vote
146 views

### "Backwards Read Primes" of Codewars powershell version

Link to problem: https://www.codewars.com/kata/5539fecef69c483c5a000015/train/powershell I have a working solution in Codewars but my solution is slow for some reason and I need to improve the ...
145 views

### Gradient descent algorithm for solving localization problem in 3-dimensional space

Task This code accomplishes the Time Difference of Arrival (TDoA) multilateration problem (see) using gradient descent (known otherwise as steepest descent). Goal I'm looking to: a) Improve speed: In ...
• 285
1 vote
1k views

### Great-circle distance in Java

Today's assignment states that I need to write a complete java program which will calculate the relative orthodromic distance between two sets of geographic coordinates declared by the user. When the ...
2k views

### Square roots via Newton's method in Java

This time, one of my labs state that I need to write a complete java program which will calculate the square root of a number via Newton's method. The reason behind using Newton's method, as opposed ...
34 views

### Data Visualization (rendering env)

How can I improve this code? It renders a ML env with openAI gym and matplotlib. I am new to coding so not sure if my variables or format or any lines can be improved. ...
• 21
4k views

### Efficiently find all the Pythagorean triplets where all numbers less than 1000

How can I make this code run faster: ... 