# 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?)

478 questions
Filter by
Sorted by
Tagged with
2k views

### Calculating exponential moving averages

I'm wanting to calculate exponential moving averages of a variable, distance. Is the logic (and math) correct in the below code? ...
337 views

### Code reduction possible with modulo operator?

I'm trying to model a puzzle in order to resolve it with the Choco solver. One of the constraint I'm coding is cyclical (it's triplet which follow themselves) like the following example: ...
1k views

### What is the most pythonic way to solve a differential equation using the Euler method?

For example, with this differential equation, how can I do it in a pythonic way? dx/dt=f(x,t) I have a way to do it, but I think this is not the most pythonic ...
958 views

### Draw the table like 6³ = 3³ + 4³ + 5³ to 100³ = 35³ + 70³ + 85³

I want to draw the table like this: ...
614 views

### Matrix multiplication of arbitrary sizes [closed]

This function is for multiplying two matrices together. It's kind of messed up because the result is actually supposed to overwrite the object that called it, or since it's ...
2k views

### Calculating Pi in Python

I have implemented a calculation of Pi in Python, just for the lulz. But now I wonder if I could improve this more. Could you help me out? I do not program a lot in Python. ...
164 views

### Return input as a partial fraction

I need to write a function that, when given an input (string, float, or int), returns that input as a reduced partial fraction, and it needs to accept a wide array of inputs: ...
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 ...
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 ...
4k 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, ...
1k views

### Calculating angle in isometric view

I have an angle where I need to convert a 'normal' angle to an isometric angle (116 degrees), and I came up with this function. It works, but I was wondering if the math could be optimized/simplified, ...
8k views

### Newton's method to solve cubic equations

I have used the Newton-Raphson method to solve Cubic equations of the form $$ax^3+bx^2+cx+d=0$$ by first iteratively finding one solution, and then reducing the polynomial to a quadratic a1*x^2+b1*x+...
2k views

### Find power of a number

Looking for optimization, smart tips and verification of complexity: O (log (base 2) power). NOTE: System.out.println("Expected 16, Actual: " + Power.pow(2, 7)); ...
104 views

### How to reduce execution time for this clustering computation?

Is there any way to reduce total execution time for the function getSilhouetteIndex? P.S. I am using weka SimpleKMeans for ...
4k views

### Python class that implements the Newton method

Here is a python function I wrote to implement the Newton method for optimization for the case where you are trying to optimize a function that takes a vector input and gives a scalar output. I use ...
166 views

### min and max simultaneously

I have two numbers: a and b. I want to put the smaller of them in one variable, and the larger in another variable (if they are equal, then any of them can be in any of the variables). Currently my ...
1k views

### Find the smallest number whose sum of digits add to a number [closed]

Request for review, optimization, best practice recommendations etc. ...
204 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:...
11k views

### Finding the Pythagorean triplet that sums to 1000

Project Euler problem 9 says: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There ...
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 ...
11k views

### Determining common divisors of two numbers

Is there a better algorithm for finding the common divisors of two numbers? Can this code be shortened? ...
53k views

### Finding divisors of a number

This code finds all the divisors of a given number. Can it be shortened? ...
165 views

### Find number matching range

I have code that checks the range of a value and sets a variable accordingly. ...
4k views

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 ...
13k views

### Taking the sum and average of the keyboard inputs

I have an assignment as follows: Write a program where you can enter from the keyboard up to 10 integers. If the number entered is equal to -99, stop reading numbers from the keyboard and ...
80k 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: ...
1k views

### Polynomial equation solver in Ruby

I've written a small function for solving simple quadratic equations: ...
2k views

### Given sum and product, find the two variables

Basically, find x and y given the product = xy and sum = x + y. I would like review on optimization, clean code, and complexity. It looks like O(1), but the while ...
840 views

### Approximating square root (loops and functions exercise)

Just looking for some feedback on my solution for Tour of Go Exercise 24 - Loops and Functions. Is there anything that looks like a wrong way to do things in go? anything I could have done better? <...
3k views

### How is my program for computing the Hardy-Ramanujan number?

I'm just wanting feedback on the efficiency, design and layout of this code if possible please. I suspect it's weird having a constructor with nothing in. I'd be grateful if it could be explained how ...
335 views

### Central Delannoy numbers

I am solving this problem on SPOJ Upper Right King (Hard): There is a king in the lower left corner of the $n \times n$ chess board. The king can move one step right, one step up or one step up-...
309 views

I wrote some math formulas in Haskell and was wondering about how to clean the code up and make it more readable. ...
233 views

### Reducing steps for long addition program

This is a program that I wrote to add two numbers together that are too large for the C language to store. It stores the numbers as strings instead and processes one digit at a time. But I suspect ...
167 views

Today I've noticed that the source code for a Boolean adder is very repetitive, so I wrote a Python script to generate the C source code. The script generates an adder of varying size depending on ...
4k views

### Testing if numbers in the array can be added up to equal the largest number in the array

Okay, here's a challenge at Coderbyte that completely stumped me. I came up with a solution, but I know it is flawed. Have the function ArrayAdditionI(arr) take ...
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 ...
9k views

### Finding greatest value in array smaller than x

Code review requested to make this code simpler, cleaner, and better. Input array is sorted. This program finds the greatest number smaller than x. So, in an ...
6k views

### Counting perfect squares

Below is the code I have written for counting perfect squares between a given lower and upper bound. I am using the following concept to solve this: Starting with 1, there are $\sqrt{m}$ square ...
729 views

### Pygame snap mouse to grid? [closed]

The game I am working with is a block game. It generates terrain and then allows you to freely mine and place blocks as you wish (sound familiar Cx) BUT! there is a problem. I have a code that snaps ...
7k views

### Best way to work out angle between points?

I have the following JavaScript code to work out the angle between two points (clockwise). The code I have seems a bit "hacky" to me, as I have a while loop ensuring the number is not negative (to ...
163 views

### Polynomial data structure

The following is working but I just want to know if I can make it better in any way. The header file can be viewed here. ...
904 views

### Simple mathematical console application

I am new to C++ and with my basic knowledge of the language, I have attempted to create a simple console application filled with lots of useful functions involving math related stuff. The following ...
91 views

### Cleaning and simplifying function that returns a number based on certain values

I have a rather clunky function that returns a number based on certain values: ...
379 views

### Simple way to decrement number within a repeating range

I currently have a solution to increment numbers within a range: i % x + 1 Where x is the maximum number in the range and <...
590 views

### Calculating the nonlinear reflection coefficient of a crystalline silicon slab

The purpose of this script is to calculate the nonlinear reflection coefficient of a crystalline silicon slab. It takes some input files (columns of data separated by whitespace), converts that data ...
987 views

### Command line calculus program

I have written a simple command line program in Python 3 that accepts a function (in the mathematical sense) from a user and then does various "calculus things" to it. I did it for my own purposes ...
97 views

### Find all subsets of a vector that sum up to another vector

I'm looking for a way to make my code more simple. This code takes a list of vectors and returns all subset of that list that sum up to another vector. For example: ...
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 ...