# Questions tagged [algorithm]

An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

5,094 questions
Filter by
Sorted by
Tagged with
1k views

### Coding exercise to represent an integer as words using python

Context: Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "...
• 103
307 views

### Determining Error Rate of Phase Shift Keying via Monte Carlo Simulation

My program calculates the symbol error rate for various signal-to-noise ratio (SNR) and modulation orders using Monte Carlo simulation on Phase Shift Keying (PSK). How can it be improved? ...
• 245
180 views

### Efficiently tagging first and last of each object matching condition

How can I make the code more readable and more efficient? (provided code is O(n²) time, but intuition says it can be pre-processed and done in O(n) time) Description it tags the first and last of ...
• 1,128
75 views

### Check which sinks are connected in the pipe system using Python

It would be very helpful to me as a beginner if I could get feedback on my code, specifically about the efficiency of algorithm used and potential improvements in code quality. Code context: There is ...
• 103
1k views

### Fast circular buffer

I'm working on a modern C++20 solution that does two things: It accumulates a sliding window of input data points; the sliding window's size is fixed and known beforehand. Data has strict ordering ...
111 views

### Advanced Python Calculator using OOP

I've been into computer science fundamentals (proper data structures/algorithms/etc) including Object Oriented Programming. Therefore, I decided to reformat my old traditional Python Calculator into ...
740 views

### Optimal Algorithm to Append and Access Tree Data

I have been studying intermediate or more advanced data structures in Python, one of which I've decided to try out is the Tree Data Structure. I've been studying the theories as well as the ...
190 views

### Pollard's rho algorithm implementation

I used std::multiset to determine how many times that factor appears. I'd like to know if it can be improved; I've focused on the algorithm rather than the code. ...
• 43
1 vote
44 views

### What is the most efficient way to figure out if a single number is prime for numbers from 2 up to 2,147,483,647 in Java?

As Java programmers, we can always use the BigInteger isProbablePrime() method or store manually all prime numbers in a HashMap. This question is about the most efficient way to figure out if a single ...
125 views

### Efficient polynomial multiplication in Python

I'm practicing problems for the ICPC competition, and one of the problems requires solving it by using an FFT to compute the product of two polynomials efficiently. Since this is for the ICPC ...
• 165
680 views

### More Square Root

This is a follow up question to my Studies on Square Roots post. I'm posting additional code for solving for square root similar to the other, but with more code to benchmark. ...
• 141
785 views

### Studies on Square Roots

This post marks the beginning of my ventures into calculating square roots. However, I have no idea how the processor, more specifically the FPU with the fsqrt ...
• 141
1 vote
109 views

### A simple performant factorial function

I am new to algorithm and data structure. After a little introduction to the topic, I have decided to implement a function called calculateFactorial, which takes an integer and calculates its ...
1k views

### Implementation of Euler-Maruyama numerical solver

I am trying to write a python implementation of Euler-Maruyama and Milstein schemes for numerically solving stochastic differential equations. The pseudo-code for the algorithms is in the Wikipedia ...
• 397
43 views

### reassigning a variable with a divide operator gives nan in program when zero is entered [closed]

Please keep in mind that I am very new to programming. I was given a the following question as my assignment in C++: Write a program that computes the value of an nth degree polynomial A(x)=a_0+...
1 vote
54 views

### Principal Variation Search (PVS) for playing Connect Four in Java

This time, I have an implementation of PVS (Principal Variation Search) algorithm for playing the game of Connect Four. The repository holding the above .java file ...
• 29.1k
1 vote
44 views

### Negamax with Alpha-beta pruning for playing Connect Four in Java

I have this repository. My main concern this time is my implementation of the Negamax algorithm with alpha-beta pruning: ...
• 29.1k
53 views

### Negamax AI for playing Connect Four against Alpha-beta pruning AI in Javascript

The working page is in GitHub. Introduction This time, I have two AI bots playing Connect Four against each other. The first AI bot uses Alpha-beta pruning, and the other one uses a Negamax with Alpha-...
• 29.1k
429 views

### Family, felon & fuzz River Crossing challenge

I first saw this challenge when a user posted it to SO about two years ago without a shred of code. The post's plea, "How do I get started?" meant the user's post didn't survive long on that ...
• 1,917
45 views

### Connect Four AI vs. AI match in Javascript

This time, I have a Javascript program that runs a Connect Four match between two Alpha-beta pruning based AI bots. (See this page.) ai-battle.html: ...
• 29.1k
564 views

### Form all possible well-formed strings of 'n' nested pairs of 2 types of ASCII brackets

Taking an interest in the challenge presented in this recent Code Review question, I've concocted a C solution (valid as C++, if I'm not mistaken) seeking to reduce both the lines of code and the ...
• 1,917
59 views

### LFU cache in Kotlin

I've been working on the classic LFU (Least Frequently Used) cache running in O(1) time problem lately and, as a student, I kind of struggled with the algorithms I found online. I got most of the idea ...
92 views

### Multithreaded Alpha-beta pruning for playing Connect Four in Java

Intro (The entire repository is in GitHub.) This time, I have parallelized the famous Alpha-beta pruning algorithm. The idea is that the parallel algorithm descends in a game tree (at least) 2 levels ...
• 29.1k
93 views

### ConnectFourFX.java - A Java FX GUI app for playing Connect Four against AI

GitHub The entire project relies here (ConnectFourFX.java) and is dependent on Connect4.java. Code com.github.coderodde.game.connect4.ConnectFourBoard.java: ...
• 29.1k
55 views

### Combining multiple regexps using the | operator and grouping the regexps by their flags

I've implemented a function that creates a multiple regular expressions predicate. The idea behind the predicate is combining regular expressions using the disjunction operator ...
277 views

### Connect4.java - The Connect Four game against an Alpha-beta pruning -based AI bot

I have this GitHub repository. It implements the command-line version of the Connect Four game. The AI bot is implemented via Alpha-beta pruning. Code ...
• 29.1k
1 vote
93 views

### Rewriting the java.util.concurrent.ConcurrentSkipListMap to a version without concurrency constructs [closed]

I have essentially rewrote the java.util.concurrent.ConcurrentSkipListMap into a version without concurrency constructs (...
• 29.1k
167 views

### Find the join of two set partitions

Two partitions of a set have a greatest lower bound (meet) and a least upper bound (join). See here: Meets and joins in the lattice of partitions (Math SE) The meet is easy to calculate. I am trying ...
• 150
69 views

### Number of partitions of (a,b) into k distinct parts which sum up to (a,b)

Problem set This is somewhat a generalization of the famous partition of integer n into k parts. Given two integers ...
558 views

### Java - Converting a skip list to the ASCII art

Intro This post won't present the actual mechanics of the skip list. Instead, all I got here is the facility for converting skip lists to funky ASCII art. For example, the unit test prints: ...
• 29.1k
505 views

### locale-aware trim functions for std::string

I have written the below two trim functions for std::string and std::basic_string that are locale-aware. Both trim all white ...
• 935
640 views

### Karatsuba algorithm in C

This is the Karatsuba algorithm I wrote: ...
• 63
225 views

### Leetcode: Number of Islands - BFS (Queue vs Recursion)

I was playing around with leetcode's Number of Islands. As per the challenge's description: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the ...
• 341
63 views

### Can I use only mutex and condition variable Instead of the Banker's Algorithm?

I've been studying the Banker's Algorithm and was curious if I could implement a similar resource management system using only Mutex and Condvar. The code I wrote is a synchronization program that ...
• 63
558 views

### C standard input escape sequence replacer program

I've been recently trying to learn C from the K&R Book The C Programming Language. Currently I'm working on my skills on program control flow, which includes case statements. To train my skills ...
• 399
96 views

### Dial's heap in Java for integer priority queues

(The entire project is here.) Intro I have this priority queue data structure for non-negative integer priority keys. I recall that it is called Dial's heap. Code Implementation ...
• 29.1k
371 views

### Fast and precise summation of random numbers

I'm trying to first formulate this challenge. In short, we want to sum up an stream of random numbers with the following objective: The objective is "simply" to sum up as many sequences as ...
• 244
92 views

### Optimizing a Function to Check Pronic Numbers in JavaScript

I've written a function in JavaScript to check whether a given number is a Pronic number. A Pronic number, also known as an oblong number, rectangular number, or ...
• 523
1 vote
50 views

### A machine learning model for predicting bit strings in Java

I have this GitHub repository (BitPredictor.java). Basically, I tried to harness a machine learning model for predicting bit strings. I have implemented it to the best of my understanding and have ...
• 29.1k
191 views

### Euler - Largest Palindrome Product in Java

Having been going over the documentation, learning more of the subtleties of Java. I am now going over some basic Project-Euler solution code I wrote a little while back. Hoping I can pick up some ...
• 750
368 views

### Write a Swift-function, which computes the Digital Root

Task: Write a function, which computes the Digital Root of a given number n. Here's the solution, which I developed: ...
• 4,368
91 views

### Rust implementation of a BTree

I'm studying rust, and I decided to implement a BTree as a way of learning the language. Can anyone give me suggestions on the code? As the language has no inheritance, and we must replace it with ...
• 176
70 views

### Clique Connect: minimum spanning tree

Problem Statement You are given a weighted undirected graph G with N vertices, numbered 1 to N. Initially, G has no edges. You will perform M operations to add edges to G. The i-th operation (1≤i≤M) ...
• 244
142 views

### Rust implementation of an algorithm I invented (RANDEVU) #2

I came up with an algorithm and created a Rust implementation of it. I've already posted it for code review previously but created another post since I've made many changes to it, including the ones ...
129 views

### Point in polygon (with holes) algorithm

I am working on a CAD addon, where I needed a function() which tells me if the point is inside a polygon or not. I have already found some good solutions for it, but none of them worked for me ...
337 views

### Codeforces: D2. Counting Is Fun (Hard Version)

The code works okay for the following problem. Problem An array 𝑏 of 𝑚 non-negative integers is said to be good if all the elements of 𝑏 can be made equal to 0 using the following operation some (...
• 244
93 views

### Image Rotation with Shear Transformation in C++

This is a follow-up question for Image Rotation and Transpose Functions Implementation in C++ and An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++. I am trying ...
• 5,572
174 views

### Snail matrix in Java - version III (generalizing)

Once again, in the previous version, Alexander Ivanchenko helped me with his awesome answer. Now, I have improved the toString() and, also, generalized the class ...
• 29.1k
328 views

### Snail matrix in Java - version II

(This post is the continuation of Snail matrix in Java. Also, this post has an continuation.) As previously, a snail matrix is any square matrix $M_n = \mathbb{N}^{n \times n}$ with cell elements ...
• 29.1k
(This post has an continuation: Snail matrix in Java - version II.) Inspired by this post, I decided to solve that problem in Java. The idea is to decide on an $n \in \mathbb{N}$, create a square ...