The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
-4
votes
0answers
15 views

Programming the infinite fraction of the golden ratio [on hold]

The infinite fraction form of the famous mathematical constant (phi) is as follows: 1+1/(1+1/(1+1/(1+1/(........ I tried to program it in C using a recursive function (and calculating the fraction ...
5
votes
2answers
171 views

Magic Square (Python)

Problem Write a method to return a boolean if an input grid is magic square. A magic square is a \$NxN\$ square grid (where N is the number of cells on each side) filled with distinct positive ...
2
votes
1answer
262 views
+50

Computational verification of Collatz conjecture using OpenCL

This code review request follows my previous request Computational verification of Collatz conjecture. Unlike the previous program (which was designed for the CPU), this code should run on modern GPUs....
5
votes
3answers
363 views

identify groups of congruent points

I have two sets of points in lists list1 and list2 given below ...
1
vote
0answers
62 views

Recursion to dynamic programming [closed]

I was trying to solve a problem on de-arrangements (Number of partial derangement such that exactly prime number discs are found away from their natural positions? (Any number of non-prime K disks ...
3
votes
2answers
116 views

Project price of publicly traded contract

I have to project the price for a publicly traded contract when the price should be approximately matching the targeted profit or loss. I have written the following code, runs under 0.02ms.. could be ...
1
vote
0answers
21 views

Solidity: verifying primality certificates, bounty for large primes

This is my first smart contract, and it isn't finished yet, but the remaining bits are complicated enough that I'd like to make sure I'm on the right track first. My concerns are: At line 117, is the ...
4
votes
1answer
58 views

Meta-circular interpreter for lambda calculus in Python

For learning purpose, I've written a simple lambda calculus interpreter (plus 'Add'). I would like it to be the cleanest and most idiomatic possible. Can we make it as neat as the Haskell version? <...
2
votes
1answer
103 views

Meta-circular interpreter for lambda calculus in Haskell

For learning purpose, I've written a simple lambda calculus interpreter (plus 'Add'). I would like it to be the cleanest and most idiomatic possible. Bonus question: how would you use ...
2
votes
1answer
57 views

Arbitrary precision Euler-Mascheroni constant via a Brent-McMillan algorithm with no math module

Utilizing the below relation, I am able to compute the Euler constant to great precision on a single thread quickly and simply. My process is to compute the natural log via the AGM(utilizing Pi an ...
1
vote
1answer
41 views

Euler-Mascheroni Single Thread Speed Improvements

The below code was written to generate γ, for educational purposes. My general methodology is as follows: Compute Gamma via the accepted answer's algorithm here. In order to do this I need to ...
4
votes
1answer
64 views

Calculate the Euler-Mascheroni constant without the math module

The below code was written to generate γ, for educational purposes. Single threaded, no functional zeroes required, no binary splitting(which can all be used to compute competitively like y-cruncher, ...
3
votes
2answers
76 views

Computational verification of Collatz conjecture using gmplib

Prerequisites The unsigned long type has a size of 64 bits. The following macros are defined. ...
3
votes
1answer
277 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
4
votes
1answer
254 views

Compute the square root of a positive integer using binary search

The requirement is to find the square root of a positive integer using binary search and the math property that square root of a number n is between ...
0
votes
1answer
97 views

Find the product of digits at odd/even places

The 'score' of a race is given. We have to calculate the 'bonus points' to be given to the participant. The '...
3
votes
1answer
29 views

GLSL Shader that displays a Rotating Rainbow Ring

I'm learning GLSL shaders on Book of Shaders and I decided to mess around with one of the examples. I wanted to see, whats a more efficient way to calculate to mask out the color? ...
2
votes
2answers
82 views

VBA root finder algorithm

I did two simple functions in VBA (see below). When a user calls TirNoPer360 in Excel he must select cashflows and dates (as range), then he will get a rate (...
7
votes
3answers
126 views

Check if an integer equals the sum of its digits to the fifth power

I can do this in one line in Python: sum([i for i in range(9,9999999) if i == sum([int(x)**5 for x in str(i)])]) Not very fast, but works nicely. I thought it ...
11
votes
1answer
58 views

Visualization of the Pythagorean theorem

Background The Pythagorean theorem asserts that for a right triangle with hypotenuse \$c\$ and other sides \$a\$ and \$b\$, the area of the square placed upon \$c\$ is equal to the sum of the areas ...
3
votes
1answer
48 views

Stern's diatomic series fusc: replacing #define functions with two return values

I have a function to review which implements Stern's diatomic series fusc for a single word (ulong = unsigned long) in place, as ...
3
votes
2answers
76 views

Gram-Schmidt process in Java for computing independent bases in linear spaces

Given a set of \$k\$ \$n\$-vectors \$\vec{x_1}, \dots, \vec{x_k}\$, Gram-Schmidt process computes a basis \$\vec{y_1}, \dots, \vec{y_m}\$ (\$m \leq k\$) the vectors of which span the same space as \$\...
0
votes
1answer
39 views

Modular multiplicative inverse in Ruby

I implemented an algorithm to find the modular multiplicative inverse of an integer. The code works, but it is too slow and I don't know why. I compared it with an algorithm I found in Rosetta Code, ...
1
vote
1answer
81 views

Sum of number's quotients when divided by exact powers of 2

Problem I have written code to solve this challenge: Given a formula: GCD(x,y) means the GCD (Greatest Common Divisor) of x and y. For example: if N=2, then: f(2) = 1/GCD(1,...
3
votes
2answers
218 views

Reverse int within the 32-bit signed integer range: \$[−2^{31}, 2^{31} − 1]\$ Optimized

LeetCode Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Feedback Optimized from beta code in the original question here. Based on this LeetCode ...
2
votes
0answers
92 views

Shutting down all lamps with minimum number of switch presses

I found an optimization problem written in Finnish but I will translate it into English. The following is an example case. I have \$n=120\$ lamps in a circle, and enumerated by \$L_1,…,L_{120}\$. ...
3
votes
0answers
61 views

Optimising multiplication of matrix rows with matrix slice/page

I've recently vectorized most of the code for an optimization problem but this one still eludes me. As the procedure and not the actual values are important here, I have replaced the original matrices ...
2
votes
0answers
115 views

Python RSA/DSA File Cryptography, Key Generation, Key Protection

I have forked a previous version of my script, critiqued here: Generate public/private keys, encrypt, decrypt, sign, verify This program allows you to encrypt and decrypt raw files using RSA keys ...
2
votes
2answers
82 views

Project Euler, Problem 273: finding perfect-square partitions

Problem: Consider equations of the form: \$a^2 + b^2 = N; 0 \leq a \leq b; a, b, N \in \mathbb{N}\$. For \$N=65\$ there are two solutions: \$a=1, b=8\$ and \$a=4, b=7\$. We call \$S(...
2
votes
2answers
108 views

PHP find cheapest permutation

I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size. For example, John needs a minimum of 10 litres of storage - it can be ...
4
votes
1answer
101 views

Faster algorithm to tailor given mathematical expression

Is there a more optimized solution to solve the stated problem? Given an array 'arr' of 'N' elements and a number 'M', find the least index 'z' at which the equation gets satisfied. [ ] is ...
5
votes
5answers
123 views

Get n-th perfect number

The task: A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. For example, given 1, you should return 19. Given ...
3
votes
2answers
90 views

Finding the number of integers between 1 and K, who's sum of the digits is a multiple of D

Good day. So, I need to find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo (10^9)+7: The sum of the digits in base ten is a multiple of D Here is my ...
4
votes
1answer
131 views

Automatic Differentiation with C++ Header-Only Library

Code review/feedback is requested and appreciated for the following open-source automatic differentiation C++ header-only library released under the Boost License. I am the author. https://github.com/...
8
votes
2answers
684 views

Balanced centrifuge configurations

Given an input N as the size of a centrifuge, I need to find out the number of balanced configurations. The full description is here. Centrifuge is a piece of ...
8
votes
1answer
208 views

Find “prime polynomials” for a user's given prime, bounds, and degree

Very amateur programmer and first-time poster here. The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list ...
3
votes
2answers
75 views

Expressing positive integers as the sum of different charming numbers

In a practice academic interview of mine, we discussed question six, round one, of the United Kingdom Mathematics Trust's 2015 British Mathematical Olympiad. Which states: A positive integer is ...
4
votes
1answer
60 views

Hungarian algorithm to perform least-cost assignments

The Hungarian algorithm solves the assignment problem, and I'm looking for any suggestions regarding improvement on my implementation (also coding style). It is based on the wikipedia entry and ...
5
votes
1answer
1k views

Determinant using Gauss Elimination

We had a question in examination to compute determinant of general square matrix. Now I had made the normal program using Laplace Formula of determinant, ie, recursive solution. Now I was thinking of ...
2
votes
2answers
519 views

Generating all permutations of 1 digit, 2 equal letters and 2 different letters efficiently

I created a function that generates all the combinations of 1 digit, 2 equal letters and 2 different letters. I reduced the numbers and letters to make it more easy to calculate: ...
4
votes
2answers
702 views

Project Euler Problem 21 in Python: Summing amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b ...
5
votes
1answer
156 views

Generating all unlabeled trees with up to n nodes

I want to visualize all unlabeled trees with \$n\$ or fewer nodes, not just count them. First idea/attempt: Take a list of all \$n-1\$ node trees, then append a new leaf to every tree in every way to ...
0
votes
1answer
116 views

Project Euler problem 12 Python

I'm new to python and programming in general and I found Project Euler problems a good way to practice python. But my code is pretty slow. It seems to work, didn't wait long enough for the code to ...
8
votes
3answers
611 views

Draw sine wave going around a circle

I have used d3 to draw a sine wave going around a circle. This is the very first time I've used d3 or drawn a SVG and I'm fairly new to JS as well, so I don't know if I've overcomplicated it/if there ...
3
votes
1answer
80 views

Counting Divisors of a number with restrictions on the divisors

Given a fraction p/q, I want to count the number of unit fraction solutions $$\frac{p}{q} = \frac{1}{u} + \frac{1}{v}$$ with some contraints $$lower \le u \le upper$$ By manipulating the first ...
4
votes
3answers
665 views

Find smallest number of squares that sum to a number

Leetcode problem 279 “Perfect Squares” is: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) I wrote the following solution: ...
1
vote
1answer
114 views

Scaling decimal numbers up within bounds of limit

This code is part of a simulation and inputs will be positive values less than one and inputs are constrained elsewhere. The goal of the algorithm is to scale the inputs up to much larger numbers, ...
2
votes
1answer
44 views

Grade of Service Probability Function Python

Consider the following typical probability scenario: I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way: <...
6
votes
3answers
1k views

Calculator of combinations without repetition

I am interested to find an absolute value (not an approximation) of "combination without repetition" for given \$n\$ and \$k\$, or \$\binom{n}{k}\$. The brute force solution would look like this <...
8
votes
1answer
455 views

Lagrange Interpolation over quadrilateral points

I've been working on a program which calculates, given a point and 4 surrounding points, the Lagrange polynomial, in order to interpolate a value. Consider that I'm not a mathematician and I better ...