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
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 ...
39
votes
3answers
9k views

Why is my C program for calculating Euler's constant of poor quality?

I was trying to post this code to a Wikipedia article, but it was soon removed. I then asked about the code in the page's talk section, and some other contributors said that is was "very poor" and ...
35
votes
9answers
7k views

Solving the Lazy Hobo Riddle

I would like help in optimizing this calculation, which is from the Lazy Hobo Riddle: There once were 4 hoboes travelling across the country. During their journey, they ran short on funds, so ...
32
votes
7answers
8k views

Checking if a number is divisible by 9

I tried to develop a new way to check if a number is divisible by 9. I have written my code and it is working fine. I'm not allowed to use *,...
29
votes
3answers
79k views

Find all subsets of an int array whose sums equal a given target

I am trying to implement a function below: Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array. For example: ...
26
votes
7answers
6k views

Python program to calculate GCD

...
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 ...
23
votes
5answers
2k views

Sums of perfect powers

...
18
votes
2answers
3k views

GCD/LCM calculator in x86 NASM assembly

After creating a similar program in C++, I've decided to try to write it in x86 assembly language (the type of assembly I was taught in college). I'd like to receive feedback regarding syntax and ...
17
votes
4answers
52k views

Finding divisors of a number

This code finds all the divisors of a given number. Can it be shortened? ...
16
votes
7answers
92k views

Euclid's Algorithm (Greatest Common Divisor)

According to Donald Knuth in "The Art Of Computer Programming", the following is how to find the greatest common divisor of two positive numbers, m and n, using Euclid's Algorithm. Divide m by n ...
15
votes
5answers
17k views

Sine function in C / C++

Due to software constraints, I cannot use the standard libraries, cmath, algorithm, templates, inline, or boost. I am also using ...
15
votes
3answers
20k 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 ...
15
votes
1answer
2k views

Multiplication 128 x 64 bits

I was fooling around with the Collatz sequence a bit and found out that long is only sufficient for starting values below ...
14
votes
5answers
126k views

Greatest common divisor

I wrote a program to find the greatest common divisor between two numbers. How do I improve this program? ...
14
votes
3answers
2k views

Wow that's a big integer! What's its largest prime factor?

I see that there are a few other people who have tackled Project Euler Problem #3. I hope you're not all sick of that question yet. I've not taken a look at those yet (purposely), but am about to now. ...
14
votes
1answer
570 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 ...
13
votes
7answers
2k views

Quadratic expression calculator

I'm hoping to get some feedback on how to improve and optimise a program I wrote which takes three values as input (via the terminal) and uses them in the quadratic formula. I'm aware this is mostly ...
13
votes
3answers
1k views

Expression template to compute the Euclidean distance

I was writing some geometry-related code again and had a closer look at my function supposed to compute the Euclidean distance between two points (N-dimensional points by the way, hence the ...
13
votes
3answers
4k views

Tonelli-Shanks algorithm implementation of prime modular square root

I did an implementation of the Tonelli-Shanks algorithm as defined on Wikipedia. I put it here for review and sharing purpose. Legendre Symbol implementation: ...
13
votes
2answers
769 views

Numerics for a game theory calculation using expected utility

I am trying to replicate Bruce B. de Mesquita's (BDM) results on political game theory for prediction. Based on where actors stand on issues, their capabilities, salience, BDM's method attempts to ...
13
votes
1answer
786 views

Temperature Interpolation

I want to interpolate temperature in correlation with height. I have temperature data from stations with coordinates and height in this format: ...
13
votes
1answer
122 views

New Moon (not Twilight) Calculator

Photography is one of my hobbies, and, living in Canada, I have the possibility of seeing, and photographing, the Aurora Borealis (Northern Lights). Now, these happen when there's ionization in the ...
12
votes
4answers
3k views

Type system for different representations of angle value

I want to implement a Type system for different representations of an angle value. Motivation to implement this as a type system comes from this question. Angle can be represented using the ...
12
votes
1answer
962 views

Parsing numbers from equations into strings

This is my first code in C++. Since I'm new to the language, I'm just looking for pointers on what can be made better. I tried to cut out unnecessary stuff, but there are some comments in there. I ...
12
votes
2answers
429 views

Find binary sequences with low peak sidelobe level of autocorrelation function

I would like to find all binary sequences from the specified range, which has low peak sidelobe level of its autocorrelation function. Here is the solution (this is simplified version of the real ...
11
votes
6answers
2k views

Beehive numbers - using goto in C++

I understand that using goto in C++ code is strictly unadvised, but sometimes, it really reduces the number of lines of code like in the following case. This is my ...
11
votes
7answers
7k 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 ...
11
votes
3answers
4k views

Determining Pythagorean triples based on the hypotenuse

Below is my written code to determine if, based on the hypotenuse, a Pythagorean triple is possible, and what are the lengths. I was wondering if there is a more efficient way of doing so. ...
11
votes
3answers
989 views

Algorithm to calculate semi perfect integers, lack of efficiency

I'm working on an algorithm to calculate weird numbers, and to do so there are several properties that needs to be calculated, one of them being, if it is NOT a semi-perfect/pseudoperfect number. My ...
11
votes
1answer
4k views

Factoring quadratic equation

I have a program I am writing that calculates Algebra 2 functions and equations. The interface is all CLI, and I even made commands that are similar to shell commands. That's not the part I want ...
11
votes
5answers
5k views

Maximum product of 3 integers in an int array

I want to find the maximum product that can be obtained from any 3 integers in an integer array. The optimal solution has time complexity of \$O(n)\$ and space complexity of \$O(1)\$. I managed to ...
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'...
11
votes
1answer
60 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 ...
11
votes
1answer
1k views

Lotto simulator

With the recent craziness of the Powerball in the US, I got interested in building a little lotto simulator to see how frequently I could win with purchasing large amounts of tickets. For those not ...
11
votes
2answers
29k views

Calculating Exponential Moving Average in Python

I'm in the process of creating a forex trading algorithm and wanted to try my shot at calculating EMA (Exponential Moving Averages). My results appear to be correct (compared to the calculations I did ...
10
votes
8answers
12k views

Calculate square of a number without multiplication

I wrote these two methods to calculate the square of a number: ...
10
votes
4answers
978 views

Solving for the zero of an arbitrary function

This methods solves for the zero of an arbitrary function using the Bisection Method. It works as desired, but I want to know if the code can be improved or made more readable and if I'm properly ...
10
votes
2answers
20k views

Pretty-print Pascal's triangle

...
10
votes
4answers
581 views

Three functions for computing simple arithmetic

Here is my code where I have 3 functions that do simple arithmetic: ...
10
votes
3answers
1k views

Finding the number of boxes that can fit into a truck using JavaScript

...
10
votes
3answers
4k views

Simple complex number class

This is my first time writing C++, so I would appreciate advice in the areas of: Code style (naming conventions, indentation, etc) Memory usage (am I performing unnecessary object copies?) Class ...
10
votes
3answers
733 views

Simple number theory game

I just started JavaScript about a week ago. For my first project, my goal was to use my knowledge of basic JavaScript to create somewhat of a "thinking computer brain," so to speak. I wasn't focused ...
10
votes
1answer
996 views

Calculating pi using bash

I've written a little program to calculate pi using the Nilakantha series: For this formula, take three and start alternating between adding and subtracting fractions with numerators of 4 and ...
10
votes
2answers
393 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. ...
10
votes
5answers
12k views

Polynomial program

The program basically takes a polynomial and does some simple calculations with it. With a couple of hours of work I managed to get my first console application to work (aside from the obligatory "...
10
votes
3answers
119 views

Reinventing the Math Functions

Just for practice in the mathematical side of programming, I decided to rewrite the math functions, with the addition of the root() function, which the Math library ...
10
votes
1answer
148 views

Gauss-Seidal implementation

The code is an implementation of the Gauss–Seidel method. I would like a general review of the code. PS: I use code::blocks IDE ...
10
votes
2answers
381 views

Project Euler Problem 530: sum of sum of greatest common divisors

Here is a description of Project Euler problem 530: Every divisor \$d\$ of a number \$n\$ has a complementary divisor \$n/d\$. Let \$f(n)\$ be the sum of the greatest common divisor of \$d\$ ...
10
votes
2answers
205 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 ...