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
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
540 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
2k 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
104 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
580 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
129 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
241 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
102 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
405 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
600 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,...
137
votes
17answers
17k 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
790 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 + ... + ...
3
votes
2answers
285 views

Zeckendorf Representation of positive integer

Zeckendorf's theorem states that all positive integers crepresented uniquely as the sum of one or more distinct Fibonacci numbers in such a way that the sum does not include any two consecutive ...
3
votes
2answers
5k views

C++ code to find distance between line and point

So I made this simple program that allows users to construct points and lines and then return the smallest distance between a given point and a line. In doing so I have used some OOP concepts and ...
2
votes
3answers
531 views

Ruby function to find numbers whose sum of squares of divisors is a perfect square

I am doing a problem my buddy gave to me and I have my solution which works but needs to be optimised. I have tried to optimise it as much as I can with my knowledge but it seems there is still room ...
5
votes
2answers
147 views

Large Power Mod - Extreme Efficiency

I've implemented my own 'power-mod' procedure which will compute a^b 'mod' n for some large b and even larger ...
2
votes
1answer
238 views

Z^m number system in Rust version 2

A \$Z^m\$ number system includes integers in the interval \$[0, m)\$ when \$m > 0\$ or \$(m, 0]\$ when \$m < 0\$. The code defines a trait Mod to represent ...
2
votes
1answer
688 views

Z^m number system in Rust

A \$Z^m\$ number system includes integers in the interval \$[0, m)\$ when \$m > 0\$ or \$(m, 0]\$ when \$m < 0\$. The code defines a trait Mod to represent ...
6
votes
2answers
91 views

Code to measure the length of binary numbers in terms of English nomenclature

"ONE" and "ZERO". The basis of this project is in this video -> links to his code can be found in description In it the speaker asks you to check his math; I wrote my own code and then compared it ...
3
votes
3answers
1k views

Lucas sequence implementation

Could you suggest any improvements in this Lucas sequence's implementation: ...
2
votes
2answers
203 views

Estimating the total production count from a few random serial numbers

This is basically my attempt to the German Tank Problem in which we are given a few serial numbers and expected to return an estimation of the total production count. Such as, if 4 tanks are captured ...
2
votes
1answer
1k views

Calculating exponential moving averages with multiples threads

I calculate the moving averages of a specific measure. However, multiples threads can access this class in order to compute the average. ...
2
votes
2answers
468 views

Calculating the statistical Mode(s) of a (possibly multi-modal) set of elements

I am trying to come up with the most performant way of calculating the statistical Modes of a std::vector, obeying two conditions: 1) my function should not ...
2
votes
1answer
347 views

Generating numbers that are a product of consecutive primes

I have implemented a correct but horribly coded solution to Project Euler Problem 293. An even positive integer N will be called admissible, if it is a power of 2 or its distinct prime factors ...
4
votes
4answers
300 views

Finding b and e such that b to the power of e is closest to a given number

The question is to find a number with absolute minimum difference from a number of the form be. I came up with the below code which takes 4 seconds to solve each test case on an average however it has ...
7
votes
2answers
1k views

Squaring the two digits in the middle of a four-digit number

I got a four-digit positive number. I need to square the two digits in middle to generate a new number on which the same procedure is applied. The numbers get saved in a list. This loops as long as ...
7
votes
2answers
203 views

Calculating the result of a recurrence function

The recurrence function is defined as follows: \$f(0) = 1\$ \$f(1) = 1\$ \$f(2n) = f(n)\$ \$f(2n+1) = f(n) + f(n-1)\$ I was tasked to calculate the recurrence of a very large number, \$n = ...
2
votes
0answers
323 views

Factoring a polynomial with integer coefficients

I haven't programmed in a while and need to improve my code in any way possible: efficiency, readability, etc. This program is for getting integer coefficients of any polynomial and factoring: ex. $$ ...
4
votes
1answer
746 views

Permutation index Python

Here is my Python code for finding the permutation index of a given digits and a target number. And by permutation index I mean, for example, given digits ...
4
votes
3answers
159 views

Time Limit Exceeded for ETF - Euler Totient Function at Spoj

In number theory, the totient φ of a positive integer n is defined to be the number of positive integers less than or equal to n that are coprime to n. Given an integer n (1 ≤ n ≤ 106), compute the ...
9
votes
3answers
1k views

Save the Prisoner

A jail has prisoners, and each prisoner has a unique id number, S, ranging from 1 to N. There are M sweets that must be distributed to the prisoners. The jailer decides the fairest way to do this is ...
7
votes
2answers
1k views

Program to find magic square of squares

I have a program which finds, well, a magic square of squares. The problem is, it's quite slow - it processes around 50 numbers in half a second when the number range is above 40,000. Is there any ...
5
votes
1answer
145 views

Generalized Project Euler 2: A sledgehammer to crack a nut

The task at hand I have previously asked about how optimizing Project Euler 1. See Generalized Project Euler 1: A sledgehammer to crack a nut. In this question I wanted to explore the next problem: ...
6
votes
3answers
654 views

Comparing algorithms for computing binomial coefficients in Java

I have these 3 different algorithms for computing binomial coefficients (I also had the 4th recursive one, yet I discarded it since it is super slow). The first uses the factorial formula, the second ...
6
votes
2answers
126 views

Finding product palindromes

I am new to perl and want some help in tuning this script if possible cause it takes time if I increased my array. also if I use warnings I get a lot of warnings ...
2
votes
1answer
71 views

Find common factor of numbers in a list

This program calculates common factor for given numbers. The original use case is to find a possible resolution for pixel games that scale well with new screen resolutions (that's why I don't have any ...
7
votes
0answers
2k views

constexpr Sin Function C++ 14

I have written a constexpr sin function using c++14 and would like to know what I can do to improve it. I am trying to balance code clarity/maintainability with ...
25
votes
4answers
1k views

Generalized Project Euler 1: A sledgehammer to crack a nut

The problem Project Euler 1 is one of the most asked questions on site. However I wanted to solve the more general problem of division. Multiples of a list If we list all the natural numbers ...
5
votes
6answers
5k views

Counting pairs of relatively prime numbers

Problem from Hacker Earth: Inverted GCD: Given an array a of \$N\$ numbers , you have to find the number of pair of indices \$i\$ and \$j\$ that satisfy the following relation: \$i <...
2
votes
2answers
76 views

Text font size adapting to space

In order to practice my programming skills, I am trying to implement a Button class, to use in pygame. I know there are libraries like PGU, I do this to improve in programming. I spent a bit of time ...
2
votes
0answers
127 views

Roots of Unity in R

I have a paper on the definition of (i) available here. I was curious about the simultaneous real numbers associated with the roots of unity, and noticed there was not a routine in R. I have this ...
5
votes
2answers
223 views

Function to check truth value of a specific statement

I want to check the truth of A + B - (A & B) == A | B for N-bit numbers. For example, here's a "truth" table for 2-bit numbers. (I know I'm taking liberty with ...
5
votes
1answer
13k views

Different neural network activation functions and gradient descent

I've implemented a bunch of activation functions for neural networks, and I just want have validation that they work correctly mathematically. I implemented sigmoid, tanh, relu, arctan, step function, ...
5
votes
1answer
145 views

Reliability polynomial calculation

The reliability polynomial (specifically, the all-terminal reliability polynomial) is, given a value \$0 \le p \le 1\$, outputs the probability that a given graph \$G\$ is connected. Note: at the ...
3
votes
1answer
80 views

Symmetry analysis for atom arrangements in a crystal

For a while now I've been meaning to post some of my Haskell code here so that someone can tell me what parts of the language/base library I've been completely overlooking. This is the first thing I'...

1 2 3 4 5 9