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,096
questions
4
votes
1
answer
122
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 ...
2
votes
1
answer
81
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 ...
7
votes
3
answers
718
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 ...
2
votes
1
answer
179
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.
...
3
votes
1
answer
118
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 ...
4
votes
2
answers
675
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.
...
8
votes
4
answers
779
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 ...
1
vote
2
answers
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 ...
6
votes
3
answers
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 ...
0
votes
1
answer
42
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
0
answers
52
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 ...
1
vote
0
answers
43
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:
...
0
votes
1
answer
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-...
6
votes
2
answers
418
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 ...
2
votes
0
answers
42
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:
...
9
votes
3
answers
547
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 ...
2
votes
2
answers
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 ...
4
votes
1
answer
91
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 ...
5
votes
1
answer
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:
...
2
votes
1
answer
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 ...
4
votes
3
answers
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
...
1
vote
1
answer
92
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 (...
2
votes
2
answers
164
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 ...
3
votes
1
answer
67
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 ...
5
votes
2
answers
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:
...
6
votes
3
answers
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 ...
6
votes
1
answer
638
views
Karatsuba algorithm in C
This is the Karatsuba algorithm I wrote:
...
3
votes
1
answer
223
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 ...
2
votes
1
answer
60
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 ...
5
votes
2
answers
557
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 ...
5
votes
0
answers
95
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
...
5
votes
1
answer
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 ...
3
votes
2
answers
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 ...
1
vote
0
answers
48
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 ...
7
votes
3
answers
189
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 ...
3
votes
2
answers
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:
...
2
votes
0
answers
87
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 ...
2
votes
1
answer
69
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) ...
2
votes
0
answers
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 ...
8
votes
1
answer
124
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 ...
6
votes
1
answer
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 (...
4
votes
0
answers
91
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 ...
3
votes
1
answer
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 ...
4
votes
1
answer
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 ...
6
votes
2
answers
879
views
Snail matrix in Java
(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 ...
14
votes
4
answers
2k
views
Create a snail matrix
Statement:
Create a script that, given an integer \$n\$, create a square matrix of dimensions \$n \times n\$ with the numbers from \$1\$ to \$n^2\$, arranged in a snail pattern.
Example:
...
5
votes
1
answer
406
views
Optimizing Perfect Hash Table Implementation in C for Improved Performance
I wrote code that implements perfect hash table, according to its description in the book "Introduction to Algorithms by Thomas H Cormen", but the code does not pass time tests in contest. I ...
-2
votes
1
answer
61
views
I wrote a O(log N) code for largest element in an array today but is this an already existing way to find max element in an array? [closed]
Largest element in an array's most optimal approach. The code I have written has passed all test cases in https://www.naukri.com/code360/problems/largest-element-in-the-array-largest-element-in-the-...
5
votes
1
answer
338
views
Leetcode: Largest Number
I was trying out leetcode's Largest Number.
As per the challenge's description:
Given a list of non-negative integers nums, arrange them such that
they form the largest number and return it. Since ...
4
votes
0
answers
78
views
Implementation of Kirkpatrick-Seidel convex hull algorithm and comparing against Jarvis-March
Introduction
I wanted to implement the Kirkpatrick-Seidel(KPS) convex hull algorithm and chose Rust as my language. I referred to the original KPS paper and tried to implement as closely it as ...