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?)

Filter by
Sorted by
Tagged with
2 votes
1 answer
43 views

Find perfect squares within a given range

I managed to make this work without breaking the program if you enter some weird numbers in the range. The thing is I know this could have been done simpler and smarter. I can't really see how right ...
user avatar
3 votes
1 answer
147 views

Applying a cost function to a string based on a pattern string

I have a cost function Cost_Func(pt,STRING) that calculates value in a specific way based on the given pattern string pt and ...
user avatar
  • 133
0 votes
1 answer
68 views

Optimizing a program that checks through a very large set of permutations

I'm not sure if this is the correct place for an optimization question like this, but I'm not even sure where to start looking. So here it goes. There's this game I play that has very simple rules. ...
user avatar
2 votes
0 answers
48 views

JavaScript functions to generate Pythagorean triples

I am sure you know what Pythagorean triples are, in the extremely unlikely case that you don't know: A Pythagorean triple consists of three positive integers a, b, and c, such that $$a^2 + b^2 = c^2$$...
user avatar
1 vote
0 answers
16 views

Iterating over de Bruijn sequences in JavaScript

I have just put this code together, it's not quite the most elegant but it seems to get the job done. Does it seem alright? I could not find anywhere a "de Bruijn sequence algorithm", so I ...
user avatar
  • 341
1 vote
1 answer
112 views

Unit tests for a 2D Vector class

I'm writing a vector class for 2D geometry applications, and below is a rough draft for a unit test using Cpputest. I'm familiar with unit tests, but this is the first I've done for a purely ...
user avatar
  • 333
6 votes
2 answers
1k views

Entropy of String in C

I have written a C program that calculates Shannon entropy for a C-String (const char *). It works well but in many ...
user avatar
1 vote
1 answer
48 views

C code for discovering numbers with interesting/beautiful/symmetric properties

Below is a C code that generates 1000 random positive integers, for each number we check primality, palindromic property, and the square root of the sum of the digits. The purpose is to find/discover ...
user avatar
  • 1,142
1 vote
0 answers
48 views

Integrate a normal map via a 2D gradient field

This code transforms a normal map into a 2D gradient field and then iteratively integrates it to a likely solution. I don't have a lot of experience with C++/CUDA yet and would appreciate some advice ...
user avatar
4 votes
4 answers
879 views

Count numbers that are the sum of three perfect cubes

From numbers from 1 to 100, count the numbers where the following equation is satisfied with 1 <= a <= 100, 1 <= b <= 100, and 1 <= c <= 100: $$a³ + b³ + c³$$ I have this: ...
user avatar
6 votes
4 answers
858 views

Converting lengths and weights using functions in C++

Question is: Write a program that asks the user if he or she wants to convert lengths or weights. If the user chooses lengths, then the program asks the user if he or she wants to convert from feet ...
user avatar
1 vote
0 answers
54 views

Haskell proof checker for FOL with equality

Is this a correct implementation in Haskell of a proof checker for first order logic with equality? I am most interested in if it is correct, and if the functions can be made clearer. I would like it ...
user avatar
3 votes
1 answer
190 views

(Google Foobar XOR Checksum Challenge) How do i optimize this function to run for much larger values?

I wrote a function that accepts 2 integers start and length as input and returns the bitwise XOR of certain numbers as described below: ...
user avatar
0 votes
1 answer
61 views

Generating floats from integers

I wanted to make an algorithm that is a counter-example of Cantor's diagonalization argument. Given an integer, this Python code will produce a unique rational number. Fed the sequence of positive and ...
user avatar
  • 451
5 votes
1 answer
59 views

Exploring the space of 8 parameters of a physics problem to then be evaluated in ODE integrator as LSODA millions of times, efficently

there's a function in one of the scripts I've been working on that I really want to improve. Disclaimer: Being honest, I'm not really very good at programming but I've been learning day to day and it'...
user avatar
  • 163
2 votes
0 answers
78 views

Maximizing Efficiency of Collatz Conjecture Program Python

My question is very simple. I wrote this program for pure entertainment. It takes a numerical input and finds the length of every Collatz Sequence up to and including that number. I want to make it ...
user avatar
  • 21
5 votes
1 answer
72 views

Find the mean magnitude of values inside unit circle

I'm fairly new to Python and am exploring how to fix functions and make code 'more effective and readable'. As a first step, I would want to try to make shorter snippets of code here - for example by ...
user avatar
  • 51
2 votes
1 answer
71 views

Cholesky decomposition

This code is Cholesky decomposition in numerical linear algebra. ...
user avatar
2 votes
1 answer
42 views

Intersection of 2D vector equation straight lines

I know the vectors that describe two lines, $$\mathbf{a} + \lambda\mathbf{d}$$ and $$\mathbf{b} + \mu\mathbf{d'}$$ I've written a JavaScript function to get the coordinates of the point of ...
user avatar
2 votes
1 answer
123 views

Simple Lagrange interpolation

I wrote a Lagrange polynomial interpolation class. It works fine. At the beginning some definitions: $$ \begin{align} &nodes:\qquad x_0, \dots, x_n \\ &values:\qquad f(x_0), \dots, f(x_n) \\ &...
user avatar
  • 123
6 votes
2 answers
443 views

Writing a C++ Matrix class

I am refreshing my C++ programming skills, and I'm currently reading the book C++ 17 by Ivor Horton. The idea is to code some naive implementations of numerical algorithms (Root finding, solving a ...
user avatar
  • 335
3 votes
1 answer
70 views

Convert number from old range to new numeric range

I have a function that takes a specific number and a range, and re-scales it to the corresponding number in a different range. ...
user avatar
  • 133
0 votes
3 answers
72 views

Print greatest factor if it is same for both numbers

Take input n1 and n2 and check if they have the same greatest factor. If they do, print that factor. If they don't, print "No". example: input: 6 9 output: 3 input: 15 27 output: No ...
user avatar
3 votes
2 answers
114 views

Python program to find all possible ways an integer num can be expressed as sum of integers between 1 and lim

This is a Python program that finds all possible ways a positive integer num can be expressed a sum of a number of integers between 1 and ...
user avatar
4 votes
2 answers
103 views

find closest distance between two numbers (looking for improvement)

Let's say I have a number set like below // 1,2,3,4,5,6,7,8 Now I want to go from 2 to 5. ...
user avatar
3 votes
3 answers
422 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 ...
user avatar
6 votes
1 answer
436 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 ...
user avatar
  • 690
1 vote
2 answers
58 views

Ask 2 terms from user and add them

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 ...
user avatar
3 votes
0 answers
67 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 ...
user avatar
  • 341
0 votes
0 answers
182 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 ...
user avatar
  • 1,142
3 votes
1 answer
224 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()...
user avatar
10 votes
5 answers
2k 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 ...
user avatar
  • 375
2 votes
1 answer
93 views

Multiplication without multiplication for numbers in range 10-199, Vedic Maths

...
user avatar
  • 175
1 vote
1 answer
33 views

Game: Predict answer based on 3 user inputs, 2 self created inputs

...
user avatar
  • 175
4 votes
1 answer
99 views

Multiply by 9 without multiplying by 9, using vedic math

...
user avatar
  • 175
0 votes
3 answers
123 views

Get the Most frequent element in an array

I have been trying to optimize this: ...
user avatar
1 vote
3 answers
77 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 ...
user avatar
0 votes
0 answers
28 views

Three Project Euler solutions in the D programming language

I recently wrote some programmes in D language to solve the first ten problems on Project Euler, but I'll be discussing only three of them (but you're free to check out my GitHub repo with all ten ...
user avatar
  • 79
1 vote
0 answers
56 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 ...
user avatar
1 vote
1 answer
53 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 \...
user avatar
  • 1,729
0 votes
0 answers
69 views

Transform inertia tensor

I am trying to translate book: Game Physics Engine Development by Ian Millington to DirectXMath and faced with the code wich transform inertia tensor. Is it right to convert code from book like that: ...
user avatar
  • 23
4 votes
2 answers
260 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 ...
user avatar
  • 191
3 votes
1 answer
561 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 ...
user avatar
8 votes
5 answers
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 ...
user avatar
  • 91
4 votes
2 answers
75 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 ...
user avatar
3 votes
2 answers
47 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 ...
user avatar
2 votes
1 answer
131 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 ...
user avatar
  • 223
14 votes
1 answer
522 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 ...
user avatar
  • 64.4k
0 votes
1 answer
77 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 ...
user avatar
  • 155
1 vote
1 answer
1k 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 ...
user avatar
  • 305

1
2 3 4 5
12