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
7
votes
2answers
306 views

Calculating a ton of mathematical constants

For a while now I have been working to construct a program to calculate a lot of mathematical constants. Before I explain, here's the code: Code ...
2
votes
1answer
528 views

Finding the maximum GCD of all pairs

The code below is for Hackerrank competition. Seems that it works correctly but performs very slowly and because of it can't pass some tests. The task is to find the maximum GCD of all possible pairs ...
2
votes
0answers
89 views

Improved Atkin-Bernstein sieve for generating primes

This is a class from my personal code library, and from a package which deals with integer sequences. It implements an interface ...
3
votes
5answers
2k views

Absolute Values and Fractions

$$ \frac{|x|-|y|}{1+|xy|} $$ How can I reduce the amount of code without losing quality? What are your tips? Your comments on the structure, logic, in general, everything. How to make the type of ...
1
vote
1answer
96 views

Slow projection maths in C++ code

I'm optimising our codebase and currently, the slowest part of the code is a method called calculateResourceZ which is used in projections. I'm looking for ...
4
votes
2answers
2k views

Sum of proper divisors of every number up to N

I wrote a function which provides the sum of proper divisors of every number up-to N (N is a natural number) and I want to improve its performance. For example, if N = 10 then the output is: ...
3
votes
1answer
2k views

I finally solved the Birthday Problem using Java

I like to put ideas and mathematics into code whenever I am interested in the subject. Ever since I was introduced to The Birthday Problem I wanted to throw it into a Java program. I started writing ...
6
votes
1answer
194 views

Coq proof that type isomorphism relationship is transitive

The following code is in Coq, a proof assistant by INRIA implemented in OCaml. I'm defining two types as being isomorphic when there exists two functions ...
2
votes
1answer
2k views

Google Foobar bomb_baby

This is the question I am facing at level 3 of Google Foobar: There are two types: Mach bombs (M) and Facula bombs (F). The bombs, once released into the LAMBCHOP's inner workings, will ...
4
votes
1answer
54 views

Homework distribution terminal app in C++

I'm working on cleaning this code up a bit and was hoping for some suggestions. Right now it works well enough on a basic level, but it doesn't check for out of bounds numbers or improper input. ...
2
votes
2answers
3k views

Finding the greatest common factor of a list of numbers in JavaScript

The goal of this code is to take an array of two or more positive integers and return their greatest common factor, or GCF. (This is the biggest number that can be divided into all the numbers in the ...
-2
votes
3answers
162 views

Find target sum of 3 from and array

Input is a sorted array and a target sum. Find all sets of 3 unique elements that sum to a target. Looking for lowest order (big O). A, B, C are the indexes on the sorted array. A < B < C ...
5
votes
3answers
496 views

C++ mathematical vector class

I plan to implement a simple simulation in C++, which requires mathematical vectors. The following header file contains the complete vector class with all it's method definitions. By overloading ...
8
votes
2answers
740 views

Searching for three positive integers as a solution to an equation

I recently ran into a question on M.SE asking, For positive integers x, y, z find a solution s.t. \$ \frac{x}{y+z} + \frac{y}{x+z} + \frac{z}{x+y} = 4 \$ so rather than think, I made a quick brute ...
2
votes
2answers
5k views

Count the number of ways an integer can be represented as a sum of consecutive positive integers

I am trying to count the number of ways an integer can be represented as sum of 2 or more consecutive positive integers. My Code is working in under 1 second for small inputs (\$\le 10^7\$) but after ...
6
votes
2answers
331 views

Optimizing special cases of modulo

I have a use case where improvements to the speed of calculating modulo for 64 bit integer dividends makes a significant difference in overall processing time. The divisor isn't known statically - it ...
6
votes
1answer
144 views

Golfed Inverse Ackermann Function [closed]

The following was posted on PPCG: Your job is to create the slowest growing function you can in no more than 100 bytes. I did some Googling, and I was interested in the Inverse Ackermann Function: ...
8
votes
4answers
7k views

Implementation of a Fraction class in Java

I am writing a bunch of Math related classes:BigInteger,Fraction,matrix,vector,polynomial,set etc.All of these classes will use each other.The classes will either work in decimal format or in Rational ...
2
votes
2answers
334 views

Convert a double to a string containing rational and surd

First off, code: ...
8
votes
1answer
1k views

Generating Uniformly Distributed Random Numbers

Edit: Dunno what PCG is and don't want to read the paper? Maybe this video of Melissa O'Neill (the author) explaining things will be palatable instead. Original: I attempted to answer this question ...
2
votes
1answer
213 views

Total number of common factors for two numbers LARGE VALUES up to 10^12

Inputs are two values \$1 \le m , n \le 10^{12}\$. I don't know why my code is taking so long for large values where the time limit is 1 sec. Please suggest some critical modifications. ...
5
votes
2answers
1k views

Inti Sets: Hackerank

Here is the original question. In order to motivate his Peruvian students, a teacher includes words in the Quechua language in his math class. Today, he defined a curious set for a given ...
9
votes
6answers
2k views

Decide whether a triangle is isosceles or not

I think I have pretty much the best code you can get when it comes to this particular task, but I'm always open to improvement. This code will check everything I can think of to make sure that it will ...
5
votes
2answers
339 views

Practical number algorithm

I am trying to write a program to find the practical numbers, from an input from \$1\$ to \$n\$. Practical numbers My code is running correctly but it is extremely slow when calculating numbers ...
1
vote
2answers
1k views

Image Compression Using the Singular Value Decomposition (SVD) with MATLAB

I'm new to both linear algebra and MATLAB. I need help with this code with the objective of compressing image using the Singular Value Decomposition (SVD). The code is currently working, if I run with ...
7
votes
1answer
144 views

Computing max t such that tanh(pi*sinh(t)/2) <1

I'm looking for a way to quickly determine that maximum value of t such that tanh(pi/2 sinh(t)) is strictly less than 1, for a ...
7
votes
2answers
142 views

4 distinct integers, whose reciprocals sum up to 1

I wrote the following short python program to solve a math puzzle, which asked for all the 4-tuples of distinct natural numbers whose reciprocals sum up to 1. ...
10
votes
2answers
201 views

Resizing a discrete uniform CSRNG distribution

I have a requirement to generate a uniform distribution of cryptographically secure random numbers. To generate these numbers I only know of the ...
10
votes
2answers
360 views

Find highly composite number using Python 3

A highly composite number (HCN) is a positive integer with more divisors than any smaller positive integer ha according to Wikipedia. Here is a YouTube video about it. ...
3
votes
2answers
54 views

Multithreaded search for solutions to an inequality

This question is related to my previous question on the brute-force search for a solution to an unsolved mathematical inequality: $$3^{k}-2^{k}\left\lfloor \left({\tfrac {3}{2}}\right)^{k}\right\...
1
vote
0answers
134 views

Quadratic equation solver in Ruby

I wrote a simple quadratic equation solver with Ruby to help me solve quadratic equations. Please tell me what I did right or wrong, and what I can do to improve it. ...
4
votes
1answer
206 views

Brute-force search for solution to an unsolved mathematical inequality

From this Wikipedia article, the following unsolved problem is presented as a result of Waring's problem: It has been proven that a finite number of k exist, and so far none are known. The follow ...
1
vote
0answers
132 views

Determine amicable pairs within confines of Θ(n)

I had to implement a program that takes a number userNum from the user and finds all perfect numbers and pairs of amicable numbers within the range \$\lbrace 2, \...
3
votes
1answer
109 views

Counting Hamiltonian triangulations (OEIS 3122)

I'm trying to translate formulas from the 3122th sequence in OEIS database, in particular this one: ...
4
votes
1answer
1k views

Arithmetic Progression

I had to write a program that would complete an arithmetic progression based on the users input. The user would provide the third term in the series, the third from the last term, and the sum of all ...
7
votes
1answer
524 views

Simple Quadratic Equation Solver

Still pretty new to coding. I posted that Twin Primes Finder the other day and you guys were a great help so thanks. Here's my quadratic equation solver: ...
4
votes
2answers
1k views

Calculating distance and angle between two points, C# program

I like to think I'm an intermediate programmer, but I learned to code with the Unity engine so I'm still getting used to not using it, and I've never had someone review my code before. I don't know ...
3
votes
0answers
103 views

Gradient computation in JavaScript

I'm working with some math-heavy code in JavaScript. I just realized that there's a render that currently takes about 30 minutes. 95% of that time is spent in a single ~40 line function that I've ...
3
votes
2answers
77 views

Number Theory Inhibitor Calculator

I've been working on generating primes and prime products quickly to aid me in my research on prime numbers, their density, etc. The answer to my Large Number Limit Extravaganza question proved to be ...
14
votes
1answer
540 views

Large Number Limit Extravaganza

I am writing a program that computes $$n-n \cdot \left(\prod_{i=1}^n (1-\frac{1}{p_i})\right)$$ which is rewritten in my code as: $$\left(1-\left(\prod_{i=1}^n(1-\frac{1}{p_i})\right)\right) \cdot ...
1
vote
2answers
127 views

Logarithmic growth counter

I'm looking for a way to have an logarithmic incremental counter. The counter goal is related to a numeric value than goes from 1 to Infinity. The counter maximum value should be 1000. The sequence ...
4
votes
1answer
3k views

Compute Gini Coefficient

Recently, I was given a math assignment to calculate Gini Indexes for a table of percent distributions of aggregate income. The table takes the form of: ...
3
votes
2answers
228 views

Time limit exceeded on finding out the GCD and LCM of a Python list

I'm doing this HackerRank problem: Consider two sets of positive integers, \$A=\{a_0, a_1, \ldots, a_{n-1}\}\$ and \$B=\{b_0, b_1, \ldots, b_{m-1}\}\$. We say that a positive integer, \$x\$, is ...
7
votes
2answers
96 views

Sum of the reciprocals of whole numbers adding to one

I have written some code which works out which sums of reciprocals of whole numbers sum to one, e.g: \$\frac 12 +\frac 13 +\frac 16 =1 \$ \$\frac 17 +\frac 18 +\frac 19 + \frac1{10} + \frac1{11}+ \...
4
votes
1answer
345 views

Display factorial with user-entered number

This needs some optimizing without any additional variables or loops. The difficult part of the task was no additional variables subroutines/functions or loops. If that seems (near) impossible then ...
1
vote
2answers
71 views

Find the highest product between elements in a set [closed]

I'm solving a programming challenge that essentially consists of finding the highest product of elements in a set. This is my current solution, and is passing all the test cases but one. The test ...
3
votes
3answers
558 views

Last five non-zero digits of a factorial in base b

This HackerRank problem (based on Project Euler problem 160) says: For any \$n\$, let \$f_b(n)\$ be the last five digits before the trailing zeroes in \$n!\$ written in base \$b\$. For example,...
136
votes
17answers
16k views

Disproving Euler proposition by brute force in C

I wrote a little bit of code to brute force disprove the Euler proposition that states: $$a^4 + b^4 + c^4 = d^4$$ has no solution when \$a\$, \$b\$, \$c\$, \$d\$ are positive integers. I'm no ...
4
votes
2answers
4k views

4x4 matrix implementation in C++

I've been doing some 3D graphics in OpenGL lately and I needed a way to work with 4x4 matrices. My implementation supports the following operations: Matrix-matrix addition. Matrix-matrix subtraction. ...
3
votes
2answers
743 views

Finding the last ten digits of \$\sum_{n=1}^{1000} n^n\$

This is my solution to Project Euler Problem 48. Problem: The series, \$1^1 + 2^2 + 3^3 + ... + 10^{10} = 10405071317\$ . Find the last ten digits of the series, \$1^1 + 2^2 + 3^3 + ... + ...