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
0answers
338 views

Matrix inverse by elimination

Because I had trouble in finding a reliable source on the internet for the matrix inverse algorithm using Gaussian elimination, I wrote one myself. Matrix.h ...
3
votes
1answer
841 views

Find LCM of two numbers

The problem is to find LCM of two numbers. I have tried to solve the problem in two ways. First by using the LCM formula : LCM (a,b)= a*b/GCD(a,b). Second, by ...
11
votes
7answers
8k views

Check if a given number N is a power of k

I am trying to solve a problem to check whether a given number N is a power of k. Example: Input 9, 3 > True // 3**2 = 9 Input 10, 2 > False Input 4096, 16 > True // 16**3 = 4096 I want to know ...
1
vote
2answers
156 views

“Disease Spread” challenge

Right now I'm solving this problem on codewars.com. I'm not sure about how to optimise the code further. Or is my algorithm inefficient? If it's an algorithmic issue, could someone help me how to go ...
3
votes
2answers
129 views

Search for solutions to quartic diophantine equation

I wrote this code to find the variables that will satisfy the diophantine equation (i.e. a^4 + b^4 + c^4 = d^4), for all variables belong to: (0 , 430000), and it takes forever to find the answer (I ...
2
votes
3answers
676 views

Project Euler 6: Sum square difference in Functional Programming (FP)

I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Project Euler: The sum of the squares of the first ten natural numbers ...
1
vote
2answers
138 views

Modular sum of square numbers in C++

So I've got an assignment which sounds like this: Given a number n, calculate the result of this sum, modulo 10,234,573: 12 + 22 + 32 + … + n2 This is what I've come up with : ...
3
votes
1answer
89 views

Program that finds the stretch factor of a parabola

Being a newcomer to C++, I decided to write a simple program that would accomplish a simple task. I settled on writing a program that, given the vertex and point on a parabola, find the "stretch ...
1
vote
0answers
482 views

Python palindrome function; is it possible to write it without using string conversion?

I hope this question is okay to ask on Code Review; this is a little function I wrote in Python 2.7.12 that checks if a number is a palindrome. It works fine but I'd like to know if there is a purely ...
6
votes
1answer
1k views

Navigating over a square spiral

I recently found adventofcode, but when I solved Day 3 in Python, I noticed that my code isn't looking very nice. The challenge is about navigating a hypothetical memory laid out in a square spiral: ...
3
votes
1answer
164 views

Maximal pythagorean triplet that sums to n

Write a function maxPythagTriple that accepts an integer n and returns the maximum product of three numbers that are both a ...
0
votes
1answer
84 views

Yaw (Z-rotation) angle linear interpolation without quaternions

I need to write a function performing linear interpolating between two Euler angles in 2D, perfectly without the use of quaternions as conversion seems to be an overkill for such a simple task. Here'...
3
votes
0answers
55 views

“Academic interest” implementation of a Finite State Machine

Context I'm playing around the concept of a Finite State Machine. While I understand the basic principles behind it, I want to implement a simple, yet generic class which could be reused in the ...
4
votes
1answer
168 views

Drawing spirals on virtual whiteboards

I have it set up so I place my mouse in the center of where I want the spiral to be, then I press enter to start the program, then define the radius initially and how much it grows each time it goes ...
5
votes
1answer
1k views

Extending Polynomial Class with math operations

Background After seeing a random coding interview question "write a program that can take the derivative of a polynomial", I decided to solve the problem using an OOP approach in Python. Then, I ...
6
votes
1answer
3k views

Hidden Markov Model with Viterbi

I have a Hidden Markov model class with basically a single method: getting the best parse of a sequence of input tokens based on Viterbi. While I have no hardcore benchmarks, I'd love some pointers ...
4
votes
1answer
130 views

Rare binary numbers

Rare numbers are the natural numbers in their binary forms in which there aren't two (or more) ones next to each other. Make a program that will give the N. rare number! For example: In case of N =...
5
votes
2answers
402 views

Find numbers that are palindromic in consecutive number bases

I want to generate numbers which are palindromic in three or more consecutive number bases in the most optimal, fastest way (up to some range). I do not count trivial one digit palindromes. (...
3
votes
1answer
226 views

Find an unknown length using sohcahtoa (Trigonometry)

Here is a calculator for an unknown side in a right-angle. Is there a more efficient way of doing this? ...
11
votes
2answers
3k views

Softmax function implementation

I'm implementing some mathematical functions right now for future use in more exciting things and wanted to know if I'm on the right track in my approach, as I don't have much C++ experience. Here I'...
2
votes
1answer
83 views

Project Euler 23 in Scala (non-abundant sums)

I recently overhauled my solution to Project Euler Problem 23 (which asks for the sum of all positive integers which cannot be written as the sum of two abundant numbers) to move it from an imperative ...
9
votes
3answers
4k views

Odd divisor summation

Given an integer, k, we define its odd divisor sum to be the sum of all k's odd divisors. For example, if k = 20, then the odd divisor sum would be 1 + 5 = 6. Similarly, if given an array of values, ...
5
votes
2answers
336 views

Python numerical integration

Could the time complexity of this definite integral algorithm be improved? ...
3
votes
1answer
73 views

Area under curve and its volume as solid of revolution

I would like to know if there is some more "elegant" way to write these 3 functions. Any tip or idea is welcome. ...
1
vote
5answers
376 views

Optimizing program to output all numbers summing to 100

I just finished my program which calculates the solutions to this problem: Outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100....
2
votes
1answer
137 views

Calculate the sum over a list of minima of two vectors

I have the following metric: def d(x, y): """The metric.""" return 1 - sum(min(xi, yi) for xi, yi in zip(x, y)) where ...
15
votes
3answers
22k views

RSA algorithm implementation in Python 3

First, a reminder of the RSA algorithm and what my program implements: Take two distinct, large primes p and q Ideally these ...
4
votes
5answers
809 views

Calculating sum of squares in C

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula: $$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\...
7
votes
1answer
254 views
7
votes
2answers
315 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
575 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
94 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
97 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
219 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 ...
3
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
55 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
4k 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
165 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
649 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
742 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
6k 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
345 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
149 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
9k 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
377 views

Convert a double to a string containing rational and surd

First off, code: ...
8
votes
1answer
2k 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
258 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. ...

1 2
3
4 5
10