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

Calculating pi by adding areas of thin rectangles

I wrote a small program for fun to try to prove Pi by taking a certain precision and radius and using it to calculate the area of the circle. My method should be giving me an area that is just ...
3
votes
0answers
823 views

Computing intersection of 2D infinite lines

I wrote a small program to compute intersection of 2D infinite lines using Boost Geometry A line is defined by two points in Line class. The ...
4
votes
1answer
181 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 ...
5
votes
2answers
8k views

Add item after every nth step

The purpose of the method is to add a list of items after every nth step (5 for example) where the input items are repeated if needed. I'm looking for an overall review and possible how to remove the ...
1
vote
0answers
747 views

Snell's law using Zoeppritz equation by matrices

I have created the following code to calculate Snell's law angles, based on Zoeppritz equations on complex plane. The code works, seems it is returning valid values, but after all the code just looks ...
4
votes
5answers
2k views

Solve 6 simultaneous equations for 8 variables puzzle with Java

I came across this mathematical puzzle, where one really cool answer was a python script to brute force answers. Here's my take, which is essentially a translation of that to Java: ...
1
vote
2answers
1k views

Calculate Kth Row of Pascal's Triangle

Purpose Implement a solution that returns the values in the Nth row of Pascal's Triangle where N >= 0. Math First three rows ...
3
votes
1answer
706 views

Generating a maximal geometric progression of integers

I'm working on a program that generates a maximal geometric progression of at least 3 integers when given a number that the greatest value shouldn't exceed. My current code works, and it's quick(ish) ...
4
votes
2answers
3k views

Implementation of exp function in C using Taylor Series expansion

I am trying to write code to calcultate ex using: $$e^x = \sum_{n=0}^\infty \frac{x^n}{n!} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \cdots $$ This is the code I have, which works ...
3
votes
0answers
70 views

Slow multiplication in a modulo

I am willing to get a linear output of a product in a ring Z/nZ For that I have successfully implemented a code that does a Product in a modulo However, it is not optimized to output the steps ...
-5
votes
2answers
500 views

Find the sum of all numbers below N which divide the sum of the factorial of their digits

\$19!\$ is a curious number, as \$1!+9!=1+362880=362881\$ is divisible by \$19\$. Find the sum of all numbers below \$N\$ which divide the sum of the factorial of their digits. Note: as \$1!,...
2
votes
1answer
6k views

Calculating distance with Euclidean, Manhattan, and Chebyshev in C#

I am by no means proficient at being able to take mathematical concepts and converting them to code. I was wondering if you guys could look at it and make suggestions on how I could fix it or if my ...
2
votes
3answers
761 views

Optimise Division function using divide and conquer, (and multiplication only)

I have the following code I am working on, but I think it can be optimised further probably by improving the min/max variable selection, (see below). The idea is to not use actual division, ('/'), to ...
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 ...
-7
votes
1answer
125 views

Python to Ruby: inconsistency with float ceil() [closed]

Just for fun, I am trying to convert the following Python from math import * print map(lambda count: ceil( 6 * cos((count + i) * pi / 5) ), range(100)) Which ...
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
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\$ ...
3
votes
1answer
210 views

Searching the number with the biggest amount of odd divisors in an interval

I wrote this program for school (first year in college) and it works, but it's too slow. I wondered if anyone could help me optimize this a bit, because I've been trying and trying, but it just won't ...
4
votes
1answer
80 views

Count solutions of a equation with a constraint

How to count the solutions of the equation \$x \cdot y + x \cdot z + y \cdot z = n, (0 \leq n \leq 10^4)\$ with the constraint that \$x \geq y \geq z \geq 0\$ ? To solve this i've isolated the z ...
6
votes
2answers
459 views

Bell numbers calculation in C

This is my implementation of the bell() function to calculate the n-th bell number in C. Along with it I made the factorial() ...
2
votes
1answer
286 views

Solving Robot Paths with Pascal's Triangle

I wanted to take on the challenge of the classic Robot Path's problem but solve it using Pascal's triangle. The approach was generate pascal's triangle, then grab the middle element from the row. I ...
4
votes
2answers
581 views

Code Golf Challenge: Calculate Phi (not Pi)

I'm a newbie to C++ (coming from C#) and I decided to complete this Code Golf challenge for fun (though I definitely wouldn't say it's short). Essentially, the challenge was to calculate the ...
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 ...
-2
votes
1answer
114 views

Countdown to January 1 2016 [closed]

Already read this, but OP is using some other method. My website finds the time till new year. But this website claims to be accurate to the tenth of a second. So what I found out was my timer and ...
1
vote
0answers
155 views

Find Euler totient of a large range of numbers

(Euler Totient Function) Is there any way to make it faster/better in asymptotic complexity? ...
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 ...
3
votes
2answers
62 views

Summations and products and factorials oh my

The proper title of this question should be "Summations and products, and factorials oh my!". After getting quite a bit of useful feedback on this question from @mjolka, and a comment, I decided that ...
2
votes
1answer
98 views

Summations and products in F#

I was bored, and looking to do something that involved anonymous functions, and it was suggested by @Quill, that I create a summation function. I decided to also include a product function as well to ...
2
votes
1answer
56 views

Determine the number of factors of a large numbers efficiently

I am trying to determine the number of factors of a large number efficiently. I have written this function but how can it be improved upon? ...
7
votes
4answers
531 views

Next multiple with only 1 and 0 as digit

Problem: Find a multiple of a given decimal number \$N\$ that looks like a binary number. The input will consist of at most \$ 2 \times 10^5 \$ lines, each line consist of an integer \$N\$ (\$0 \lt N ...
8
votes
1answer
2k 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 ...
6
votes
0answers
646 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. ...
3
votes
1answer
1k views

Simplifying Radical Expressions

I'm working on a program to handle simplifying square roots (radicals). My biggest issue is, in order to stay precise, I can't have input any longer than 15 digits. Whenever a number is longer than 15 ...
2
votes
2answers
362 views

Project Euler #12 Java implementation

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, ...
5
votes
1answer
100 views

Baby Step Giant Step Discrete Log Solver

I've made a concurrent implementation of the Big Step Giant Step discrete log solver in Go. I'm pretty new to this branch of math, but it seems slow. Any way to speed it up or is this the limit of ...
1
vote
1answer
287 views

Project Euler #11 Java implementation

Project Euler 11 Solution in Java. The problem is at https://projecteuler.net/problem=11 package coderbyte; public class Java { ...
5
votes
1answer
2k views

Rational Polynomial Factoring method

I have written a class containing methods to factor a polynomial equation using the p over q method. The method returns a string that is the factored equation. Here is the class (can also be found ...
5
votes
0answers
131 views

Kernel density regression in Julia

Here is a naive implementation of a kernel density regression algorithm in Julia. The code works, but there is quite a bit of room for improvement though. First, there is not an optimal bandwidth ...
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 ...
2
votes
1answer
652 views

Menu driven program to represent polynomials as a data structure using arrays

Write a menu-driven program to represent Polynomials as a data structure using arrays and write functions to add, subtract and multiply two polynomials; multiply a polynomial with a constant, find ...
5
votes
1answer
230 views

Project Euler, Challenge #12 in Swift

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, ...
5
votes
1answer
221 views

Untouchable Numbers

I wrote this program in response to a CodeGolf challenge that required generating this sequence of "untouchable" numbers. This sequence can be found on OEIS as A005114. My initial implementation was ...
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 ...
1
vote
1answer
111 views

Decomposing a number as a sum of two products using tree recursion

Below is the problem taken from page6 here. The TAs want to print handouts for their students. However, for some unfathomable reason, both the printers are broken; the first printer only prints ...
1
vote
1answer
152 views

Vectorizing in Python [closed]

I have the following code: ...
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 ...
7
votes
1answer
99 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) = ...
2
votes
3answers
108 views

Algorithm to follow a point

This code basically makes the dot follow a certain point on the screen. It consumes a significant amount of computing time in my game, so I'd like it to be best ...
2
votes
2answers
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 ...
2
votes
3answers
97 views

Calculating e^x by math.h and by own means

For this program, the user needs to enter an exponent and the program will calculate \$e\$ (Euler's number) to the power of the exponent the user inputs. This is done by two ways: By the math.h ...