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
1 vote
1 answer
90 views

Sum of number's quotients when divided by exact powers of 2

Problem I have written code to solve this challenge: Given a formula: GCD(x,y) means the GCD (Greatest Common Divisor) of x and y. For example: if N=2, then: f(2) = 1/GCD(1, 4) + 2/GCD(2, 4) + 3/...
  • 113
3 votes
2 answers
5k views

Reverse int within the 32-bit signed integer range: \$[−2^{31}, 2^{31} − 1]\$ Optimized

LeetCode Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Feedback Optimized from beta code in the original question here. Based on this LeetCode ...
  • 1,017
2 votes
0 answers
96 views

Shutting down all lamps with minimum number of switch presses

I found an optimization problem written in Finnish but I will translate it into English. The following is an example case. I have \$n=120\$ lamps in a circle, and enumerated by \$L_1,…,L_{120}\$. ...
3 votes
0 answers
64 views

Optimising multiplication of matrix rows with matrix slice/page

I've recently vectorized most of the code for an optimization problem but this one still eludes me. As the procedure and not the actual values are important here, I have replaced the original matrices ...
  • 131
2 votes
0 answers
704 views

Python RSA/DSA File Cryptography, Key Generation, Key Protection

I have forked a previous version of my script, critiqued here: Generate public/private keys, encrypt, decrypt, sign, verify This program allows you to encrypt and decrypt raw files using RSA keys ...
2 votes
2 answers
155 views

Project Euler, Problem 273: finding perfect-square partitions

Problem: Consider equations of the form: \$a^2 + b^2 = N; 0 \leq a \leq b; a, b, N \in \mathbb{N}\$. For \$N=65\$ there are two solutions: \$a=1, b=8\$ and \$a=4, b=7\$. We call \$S(...
2 votes
2 answers
206 views

PHP find cheapest permutation

I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size. For example, John needs a minimum of 10 litres of storage - it can be ...
  • 21
4 votes
1 answer
113 views

Faster algorithm to tailor given mathematical expression

Is there a more optimized solution to solve the stated problem? Given an array 'arr' of 'N' elements and a number 'M', find the least index 'z' at which the equation gets satisfied. [ ] is ...
  • 141
6 votes
5 answers
382 views

Get n-th perfect number

The task: A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. For example, given 1, you should return 19. Given ...
  • 3,973
3 votes
2 answers
349 views

Finding the number of integers between 1 and K, who's sum of the digits is a multiple of D

Good day. So, I need to find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo (10^9)+7: The sum of the digits in base ten is a multiple of D Here is my ...
4 votes
1 answer
380 views

Automatic Differentiation with C++ Header-Only Library

Code review/feedback is requested and appreciated for the following open-source automatic differentiation C++ header-only library released under the Boost License. I am the author. https://github.com/...
  • 143
8 votes
3 answers
1k views

Balanced centrifuge configurations

Given an input N as the size of a centrifuge, I need to find out the number of balanced configurations. The full description is here. Centrifuge is a piece of ...
8 votes
1 answer
311 views

Find "prime polynomials" for a user's given prime, bounds, and degree

Very amateur programmer and first-time poster here. The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list ...
  • 183
3 votes
2 answers
152 views

Expressing positive integers as the sum of different charming numbers

In a practice academic interview of mine, we discussed question six, round one, of the United Kingdom Mathematics Trust's 2015 British Mathematical Olympiad. Which states: A positive integer is ...
4 votes
1 answer
75 views

Hungarian algorithm to perform least-cost assignments

The Hungarian algorithm solves the assignment problem, and I'm looking for any suggestions regarding improvement on my implementation (also coding style). It is based on the wikipedia entry and ...
5 votes
1 answer
3k views

Determinant using Gauss Elimination

We had a question in examination to compute determinant of general square matrix. Now I had made the normal program using Laplace Formula of determinant, ie, recursive solution. Now I was thinking of ...
  • 103
2 votes
2 answers
965 views

Generating all permutations of 1 digit, 2 equal letters and 2 different letters efficiently

I created a function that generates all the combinations of 1 digit, 2 equal letters and 2 different letters. I reduced the numbers and letters to make it more easy to calculate: ...
  • 237
5 votes
2 answers
3k views

Project Euler Problem 21 in Python: Summing amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b ...
  • 87
5 votes
1 answer
763 views

Generating all unlabeled trees with up to n nodes

I want to visualize all unlabeled trees with \$n\$ or fewer nodes, not just count them. First idea/attempt: Take a list of all \$n-1\$ node trees, then append a new leaf to every tree in every way to ...
  • 258
0 votes
1 answer
153 views

Project Euler problem 12 Python

I'm new to python and programming in general and I found Project Euler problems a good way to practice python. But my code is pretty slow. It seems to work, didn't wait long enough for the code to ...
  • 87
11 votes
3 answers
2k views

Draw sine wave going around a circle

I have used d3 to draw a sine wave going around a circle. This is the very first time I've used d3 or drawn a SVG and I'm fairly new to JS as well, so I don't know if I've overcomplicated it/if there ...
3 votes
1 answer
100 views

Counting Divisors of a number with restrictions on the divisors

Given a fraction p/q, I want to count the number of unit fraction solutions $$\frac{p}{q} = \frac{1}{u} + \frac{1}{v}$$ with some contraints $$lower \le u \le upper$$ By manipulating the first ...
  • 3,032
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: ...
1 vote
1 answer
135 views

Scaling decimal numbers up within bounds of limit

This code is part of a simulation and inputs will be positive values less than one and inputs are constrained elsewhere. The goal of the algorithm is to scale the inputs up to much larger numbers, ...
user avatar
2 votes
1 answer
93 views

Grade of Service Probability Function Python

Consider the following typical probability scenario: I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way: <...
  • 247
7 votes
3 answers
3k views

Calculator of combinations without repetition

I am interested to find an absolute value (not an approximation) of "combination without repetition" for given \$n\$ and \$k\$, or \$\binom{n}{k}\$. The brute force solution would look like this <...
  • 912
8 votes
1 answer
605 views

Lagrange Interpolation over quadrilateral points

I've been working on a program which calculates, given a point and 4 surrounding points, the Lagrange polynomial, in order to interpolate a value. Consider that I'm not a mathematician and I better ...
  • 131
1 vote
2 answers
882 views

Find coprime numbers less than n

I get the coprime numbers less than n with the following algorithm: ...
  • 119
3 votes
3 answers
196 views

Return the product of the pythagorean triplet if a+b+c == 1000

I have found the answer to the problem I am trying to solve, but I am curious if there can be any improvements made to my code. ...
  • 1,080
7 votes
2 answers
2k views

Class Matrix implementation

Following the 18.06 Linear algebra course, I was curious to reinvent the matrix class and basic functionality, like \$PA=LU \$ decomposition, Gauss elimination, finding inverse matrix etc. Any ...
  • 912
2 votes
2 answers
2k views

Math Problem generator using switch statement ,Random method and arrays in Java

I m new in java and created a small project in which you have to solve random math problems there is three levels in it Easy, Medium , Hard, if you win or loss in game the message for three level will ...
  • 163
27 votes
7 answers
8k views

Python program to calculate GCD

...
6 votes
4 answers
599 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 ...
5 votes
1 answer
222 views

Convert numbers from base X to base Y

I recently wrote a piece of code to convert between two bases of X, (Where 26 > X > 0). I was mighty proud that I did this. And then I started wondering, am I using modern coding conventions/practices?...
4 votes
1 answer
496 views

Method to return center items in a 2D array

I wish to have code that returns the center items of a 2d grid. Because I need an average value. The code I came up with does what I want (see the unit tests beneath it). But I think the code is ugly ...
4 votes
1 answer
2k 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 ...
  • 315
0 votes
3 answers
575 views

Project Euler #12 (Highly divisible triangular numbers) in Python 3.x

I'm a beginner to programming and just started python, I've been trying to work through Project Euler challenges. I wrote a program to solve Project Euler #12, which asks for the smallest number of ...
4 votes
1 answer
227 views

Math Vector Header Library Templated on Data Type and Size

I have created a vector math header library where the size and data type stored are template values, and I'd like it peer-reviewed please. I have a few main questions: Will using my SizedVectorBase ...
  • 41
1 vote
1 answer
446 views

Find GCD of two numbers without using multiplication, division or modulus operators

If either a or b is 0 return the other number. If both even return gcd of a/2 and b/2. If either a or b is even divide it by 2 and return gcd of the pair. If a less than or equal to b, return the gcd ...
  • 373
3 votes
1 answer
442 views

Trigonometric Subplots with Matplotlib

The code consists of 4 functions that result in a figure with 3 subplots: a sine wave a cosine wave and the superposition of both (a bichromatic wave). I have shown in 4 ways, the first two are ...
  • 1,132
4 votes
4 answers
1k views

Power of two integers

Challenge: Given a positive integer which fits in a \$32\$ bit signed integer, find if it can be expressed as \$A^P\$ where \$P > 1\$ and \$A > 0\$. A and P both should be integers. ...
5 votes
1 answer
750 views

Matrix implementation with Rank and Inverse calculation using Gaussian Elimination in Java

Disclaimer Some things that I must state: This is just for practice. I want to know the various algorithms in linear algebra in detail. I will not use this in a practical scenario. Except for ...
3 votes
1 answer
151 views

Sum the remainders of ai%aj given an array "a"

Given an array of numbers, I am creating a table where, the cell at the intersection of row i and column j contains a number ai % aj. Numbers in the array can occur multiple times. Example: For ...
2 votes
1 answer
59 views

Make a given number by adding given numbers

I have created a code that checks if a number can be made by adding different quantities of given numbers and if so returns the quantities. Is this the best way to do this or is there a more efficient ...
5 votes
2 answers
720 views

HackerRank - Non-Divisible Subset

In this problem we are asked: Given a set, S, of n distinct integers, print the size of a maximal subset, S', of where the sum of any two numbers in it is not evenly divisible by k. Code My idea is ...
  • 783
0 votes
1 answer
201 views

Greedy adaptive dictionary (GAD) for supervised machine learning [closed]

For my project in machine learning supervised, I have to simplify a training-data and I have to use this technique at page 5 of the document. Pseudocode algorithm My code (numbers are the steps): <...
2 votes
1 answer
1k views

First n Happy Numbers

I wrote a piece of code to generate a list of \$n\$ Happy Numbers, would love to see how I can write it better. More information below. ...
2 votes
0 answers
399 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
1 answer
2k 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
7 answers
11k 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
3 4
5
6 7
13