Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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

6
votes
4answers
306 views

Add Two Numbers given in Reverse Order from a Linked List

Problem Description You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add ...
3
votes
2answers
87 views

Find smallest sum of squares to make 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
1answer
35 views

Project Euler 41 Generating Pandigital Numbers [closed]

Project Euler question 41: We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. ...
1
vote
1answer
41 views

ROW(Race of work) simulation

I wrote a ROW(Race of work) simulation, the idea contains a mathematical bug and, I asked a question about it here. I would also like to make this code more readable and, this is why I am sharing it ...
2
votes
1answer
108 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, ...
3
votes
1answer
29 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: <...
6
votes
3answers
166 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 <...
8
votes
1answer
228 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 ...
1
vote
2answers
94 views

Find coprime numbers less than n

I get the coprime numbers less than n with the following algorithm: ...
3
votes
3answers
70 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. ...
5
votes
1answer
168 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 ...
2
votes
2answers
61 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 ...
6
votes
4answers
438 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
1answer
95 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
1answer
173 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 ...
3
votes
1answer
82 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 ...
0
votes
2answers
142 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
1answer
76 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 ...
1
vote
1answer
64 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 ...
3
votes
1answer
45 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 ...
4
votes
4answers
400 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
1answer
134 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
1answer
76 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
1answer
54 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 ...
0
votes
0answers
39 views

Determine minimum number of iterations to form a complete residue system

Given constant integers N and K, where 1<=N<=10^16 and 1<=K<= 10^16, the output should be the minimum integer A such that the set of integers that can be written as (K+1)m+a mod N where a ...
5
votes
2answers
141 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 ...
0
votes
1answer
98 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): <...
3
votes
1answer
213 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
0answers
163 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 ...
1
vote
1answer
293 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 ...
7
votes
6answers
2k 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
vote
2answers
100 views

“Disease Spread” challenge

Right now I'm solving this problem on codewars.com. I'm not sure about how to optimise the code further. Or is my algorithm inefficient? If it's an algorithmic issue, could someone help me how to go ...
3
votes
2answers
101 views

Search for solutions to quartic diophantine equation

I wrote this code to find the variables that will satisfy the diophantine equation (i.e. a^4 + b^4 + c^4 = d^4), for all variables belong to: (0 , 430000), and it takes forever to find the answer (I ...
2
votes
3answers
119 views

Project Euler 6: Sum square difference in Functional Programming (FP)

I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Project Euler: The sum of the squares of the first ten natural numbers ...
1
vote
2answers
109 views

Modular sum of square numbers in C++

So I've got an assignment which sounds like this: Given a number n, calculate the result of this sum, modulo 10,234,573: 12 + 22 + 32 + … + n2 This is what I've come up with : ...
3
votes
1answer
64 views

Program that finds the stretch factor of a parabola

Being a newcomer to C++, I decided to write a simple program that would accomplish a simple task. I settled on writing a program that, given the vertex and point on a parabola, find the "stretch ...
1
vote
0answers
166 views

Python palindrome function; is it possible to write it without using string conversion?

I hope this question is okay to ask on Code Review; this is a little function I wrote in Python 2.7.12 that checks if a number is a palindrome. It works fine but I'd like to know if there is a purely ...
6
votes
1answer
745 views

Navigating over a square spiral

I recently found adventofcode, but when I solved Day 3 in Python, I noticed that my code isn't looking very nice. The challenge is about navigating a hypothetical memory laid out in a square spiral: ...
3
votes
1answer
55 views

Maximal pythagorean triplet that sums to n

Write a function maxPythagTriple that accepts an integer n and returns the maximum product of three numbers that are both a ...
0
votes
1answer
32 views

Yaw (Z-rotation) angle linear interpolation without quaternions

I need to write a function performing linear interpolating between two Euler angles in 2D, perfectly without the use of quaternions as conversion seems to be an overkill for such a simple task. Here'...
3
votes
0answers
53 views

“Academic interest” implementation of a Finite State Machine

Context I'm playing around the concept of a Finite State Machine. While I understand the basic principles behind it, I want to implement a simple, yet generic class which could be reused in the ...
4
votes
1answer
63 views

Drawing spirals on virtual whiteboards

I have it set up so I place my mouse in the center of where I want the spiral to be, then I press enter to start the program, then define the radius initially and how much it grows each time it goes ...
5
votes
1answer
245 views

Extending Polynomial Class with math operations

Background After seeing a random coding interview question "write a program that can take the derivative of a polynomial", I decided to solve the problem using an OOP approach in Python. Then, I ...
4
votes
1answer
458 views

Hidden Markov Model with Viterbi

I have a Hidden Markov model class with basically a single method: getting the best parse of a sequence of input tokens based on Viterbi. While I have no hardcore benchmarks, I'd love some pointers ...
4
votes
1answer
118 views

Rare binary numbers

Rare numbers are the natural numbers in their binary forms in which there aren't two (or more) ones next to each other. Make a program that will give the N. rare number! For example: In case of N =...
5
votes
2answers
197 views

Find numbers that are palindromic in consecutive number bases

I want to generate numbers which are palindromic in three or more consecutive number bases in the most optimal, fastest way (up to some range). I do not count trivial one digit palindromes. (...
3
votes
1answer
66 views

Find an unknown length using sohcahtoa (Trigonometry)

Here is a calculator for an unknown side in a right-angle. Is there a more efficient way of doing this? ...
10
votes
2answers
792 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'...
2
votes
1answer
63 views

Project Euler 23 in Scala (non-abundant sums)

I recently overhauled my solution to Project Euler Problem 23 (which asks for the sum of all positive integers which cannot be written as the sum of two abundant numbers) to move it from an imperative ...