Skip to main content

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
13 votes
7 answers
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 ...
user avatar
5 votes
4 answers
2k views

Project Euler #5 - Lowest common multiple of 1 through 20

This is my code. All comments welcome. Last time run it only took 335 milliseconds: ...
user avatar
3 votes
1 answer
69 views

Find perfect and amicable numbers

I've been playing with perfect, amicable and social numbers lately, and created a class for investigating these. At present, it has functions to return the perfect and amicable numbers in a specified ...
Toby Speight's user avatar
  • 77.1k
151 votes
17 answers
19k 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 ...
Aidenhjj's user avatar
  • 2,373
16 votes
1 answer
780 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 ...
Linus Rastegar's user avatar
12 votes
4 answers
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 ...
diimdeep's user avatar
  • 221
10 votes
2 answers
21k views

Pretty-print Pascal's triangle

...
matheussilvapb's user avatar
5 votes
4 answers
2k views

C++ determinant calculator - follow-up

After following some suggestions you can find here, I'd like to show you the result: ...
user avatar
4 votes
1 answer
379 views

Mendelian inheritance

I started out with the Rosalind problems a while back. Turns out it's a great way to learn a new language. I'm still trying to learn Ruby, and while I haven't managed to use regular expressions as ...
Mast's user avatar
  • 13.4k
3 votes
1 answer
405 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
DaBler's user avatar
  • 309
3 votes
2 answers
357 views

BigRational based on BigIntegers

For Project Euler I've implemented a BigRational. The functionality is rather limited, just things I needed or will probably need soon. The style departs a bit from ...
maaartinus's user avatar
  • 13.5k
36 votes
9 answers
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 ...
user avatar
34 votes
8 answers
12k 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 *,...
kapil.thakar's user avatar
26 votes
4 answers
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 ...
N3buchadnezzar's user avatar
17 votes
7 answers
93k 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 ...
Fiddling Bits's user avatar
15 votes
5 answers
22k 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 ...
Elpezmuerto's user avatar
15 votes
4 answers
4k views

Calculator in C++

I made a calculator in C++. I wanted to ask is this code well written, understandable, and alright. ...
XYBOX's user avatar
  • 69
15 votes
3 answers
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. ...
RubberDuck's user avatar
  • 30.8k
14 votes
1 answer
1k views

C++ class to create and evaluate Chebyshev approximations of arbitrary functions

I recently needed to create a function to approximate a complex trigonometric function on an embedded system without a floating point unit and without a fast trigonometric library. So I pulled out my ...
Edward's user avatar
  • 66.5k
12 votes
6 answers
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 ...
user43115's user avatar
  • 329
10 votes
2 answers
6k views

Fibonacci generator with Golang

This is my Fibonacci generator: package main import "fmt" func main() { for i, j := 0, 1; j < 100; i, j = i+j,i { fmt.Println(i) } } It's ...
Goku's user avatar
  • 203
10 votes
2 answers
527 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\$ ...
Thev's user avatar
  • 251
9 votes
6 answers
3k 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 ...
Foxes's user avatar
  • 419
8 votes
1 answer
3k views

Faster(?) factorial calculations

I saw this question, but was unable to answer because of my inexistant knowledge of Rust, so I decided to try and write an algorithm on my own and put it for review. There is the casual way to ...
IEatBagels's user avatar
  • 12.4k
8 votes
1 answer
4k 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 ...
Alex Zywicki's user avatar
8 votes
4 answers
8k views

Pi-calculating program

I saw this question and answer about calculating pi on Stack Overflow, and I decided to write my own program for calculating pi. I used Python and only integers (I didn't want to use floating point ...
Uri's user avatar
  • 181
7 votes
2 answers
2k views

Helper function to solve Project Euler question 26

Project Euler problem 26 asks us to: Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. I wrote this function in Python to find the ...
ajay's user avatar
  • 295
7 votes
3 answers
259 views

Project Euler #5 (LCM 1 - 20) Follow up

I'm actually spending time on it, unlike the first time and going through, understanding and implementing the several suggestions I've landed on two different approaches. Approach 1: Employing Java 8 ...
Legato's user avatar
  • 9,829
7 votes
2 answers
2k views

Simplified Bin Packing using JavaScript

This code determines the maximum number of boxes (one size) that can fit into the back of a truck, checking all possible orientations to ensure maximum Bin Packing excitement. This is (hopefully) the ...
DatLumberZach's user avatar
7 votes
1 answer
3k views

How do I optimize this Java cube root function for BigInteger?

I find that this function is one of the biggest causes of slow program execution. I can write a square root version with BigInteger only, but with the cube root, the algorithm sometimes gets caught in ...
Brian J. Fink's user avatar
7 votes
4 answers
821 views

Are there any bugs or ways to make my divisor code better?

After coding my divisor code which I did as a practice for my GCSE stuff which I have to take soon, I have decided to ask the community if there are any bugs or ways I can improve my code. ...
Oliver Perring's user avatar
7 votes
2 answers
3k 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 ...
Emma's user avatar
  • 3,527
7 votes
1 answer
124 views

Weighted phase Laplacian formula

We have the following equation (weighted phase Laplacian): \$c_{i,j} = U(i,j)\Delta^{^x}_{i,j} - U(i-l, j)\Delta^{x}_{i-1,j} + V(i,j)\Delta^{y}_{i,j} - V(i,j-1)\Delta^{y}_{i,j-1}\$ Where \$U(i,j) = ...
FacundoGFlores's user avatar
6 votes
1 answer
801 views

Linear algebra, reduced row echelon form - function 1

This is a derivative post from here. This is just a general review, so the question is the same: Is there something... That you would consider as a bad practice and why? That is just bad in some ...
user1235831's user avatar
6 votes
2 answers
112 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 ...
Kyle Johnson's user avatar
6 votes
4 answers
1k views

Sum of digits in a^b

I'm trying to calculate a problem which requires sum of digits in \$a^b\$, where \$0 \le a \le 9\$ and \$0 \le b \le 4000\$. I have seen various other posts similar to this, but I haven't acquired ...
Prakhar Awasthi's user avatar
6 votes
4 answers
612 views

Pascal Triangle calculation with BigIntegers

On a well known programming challenges website(CW) there is a Kyu2 eggs height problem that can be solved utilizing a variation of Pascals Triangles properties. The problem asks to solve ...
Miguel_Ryu's user avatar
6 votes
1 answer
630 views

Project Euler #5 (Smallest multiple) in Swift

I found this one a little bit easier than the previous problems, but I was already familiar with the GCD/LCM formulas. ...
nhgrif's user avatar
  • 25.3k
5 votes
3 answers
2k 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: ...
loremIpsum1771's user avatar
5 votes
1 answer
67 views

Exploring the space of 8 parameters of a physics problem to then be evaluated in ODE integrator as LSODA millions of times, efficently

there's a function in one of the scripts I've been working on that I really want to improve. Disclaimer: Being honest, I'm not really very good at programming but I've been learning day to day and it'...
nuwe's user avatar
  • 163
5 votes
1 answer
225 views

Optimising Funny Marbles

Problem statement is at http://www.codechef.com/DEC13/problems/MARBLEGF Lira is given array A, which contains elements between 1000 and 2000. Three types of queries can be performed on this array:...
user2369284's user avatar
5 votes
1 answer
5k views

Project Euler Problem 12 - Highly Divisible Triangular Number - Python Solution Optimization

Project Euler Problem 12 asks (paraphrased): Considering triangular numbers Tn = 1 + 2 + 3 + … + n, what is the first Tn with over 500 divisors? (For example, T7 = 28 has six divisors: 1, 2, 4, 7, ...
rcbevans's user avatar
  • 153
5 votes
4 answers
5k views

Project Euler #5 (LCM of 1-20)

Challenge: 2520 is the smallest number divisible by 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by 1-20? Solution: ...
Legato's user avatar
  • 9,829
5 votes
0 answers
895 views

Recursive factorial calculations in Rust

I am trying to calculate very large factorials. I am looking to speed it up as 10000! takes approximately a minute and a half. ...
Jared Mackey's user avatar
5 votes
1 answer
2k views

Linear algebra, reduced row echelon form

The code that I am sharing here for you to review today, is a segment of a JavaScript library that I am going to write as time goes by for fun. It is only the two functions in the following code: <...
user1235831's user avatar
5 votes
2 answers
2k views

Speedup for Project Euler 44 - pentagon numbers

I'm trying to solve the problems of Project Euler with Haskell within less than 10s with optimized code (ghc -O2). Unfortunately, I'm struggling to find a correct ...
Stefan's user avatar
  • 203
4 votes
1 answer
1k views

Followup: How do I optimize this Java cube root function for BigInteger?

Followup to How do I optimize this Java cube root function for BigInteger? So I've tried several implementations of this algorithm. The version using only BigInteger sometimes results in a never-...
Brian J. Fink's user avatar
4 votes
1 answer
3k views

Circle - Line segment collision

I wrote an implementation of circle - line segment collision detection algorithm. Here is quick demonstration how it works: And here is my implementation in Javascript using HTML canvas: index.html ...
Tomek's user avatar
  • 315
4 votes
1 answer
666 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, ...
FodderOverflow's user avatar
4 votes
1 answer
267 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 ...
esote's user avatar
  • 3,770