All Questions
Tagged with c# mathematics
51
questions
5
votes
2
answers
264
views
C# console app that computes the Jaccard Index
So I wrote a basic C# console application that computes the Jaccard Index. This has its applications in cybersecurity and machine learning. Given two sets X and Y, the basic formula is:
$$ J(X, Y) = \...
1
vote
0
answers
147
views
Interleave Bits Generically
This commonly referenced "bit twiddling" site has an interesting algorithm for interleaving the bits of two 32-bit integers. Here is my attempt at completely generalizing the algorithm using ...
2
votes
2
answers
133
views
generate prime factors via wheel factorization
I want to factor random values of n that range from uint.MinValue to uint.MaxValue as ...
4
votes
2
answers
204
views
32-bit deterministic Miller-Rabin primality test
Are there any ways in which this code could be improved? I'm particularly interested in the ModularExponentiation function as it is the primary bottleneck during ...
5
votes
4
answers
4k
views
Dividing two numbers then handle the divide by zero exception with try/catch
I am new to coding, I hope you can help me to improve my code :)
First of all: The code works correctly.
...
3
votes
1
answer
337
views
Generic Integer Square Root
I have implemented an integer square root function that is branch-free and runs in constant time, using the first variant found in this answer as a base. All possible values for the types ...
2
votes
1
answer
268
views
Compute the derivative of a function
1.11 Arranging the derivative of a function
The derivative of any function f (x) at x0 can be estimated according to the following formula:
Write a program that prints three columns of ...
3
votes
1
answer
414
views
Rock Paper Scissors without arrays and if-statements, how to reduce
As working on a little private programming challenge, I tried "Rock Paper Scissors" without arrays or if-statements. It works as intended, though, I am sure this can be done a little bit more ...
6
votes
1
answer
747
views
C# random math questions project
I made a C# console math project where the user answers addition, subtraction, multiplication, division, power or square-root questions based on the difficulty they choose!
However, I am struggling ...
2
votes
2
answers
463
views
C# maths quiz with user choosen difficulty-level
I have built a maths project that asks the user an addition, subtraction, multiplication, division, power and square root questions based on the difficulty level they choose!
But I am trying to ...
6
votes
4
answers
263
views
All your Base - Exercism.io
This is an exercise on the website Exercism. I am self taught trying to learn C# and C++. This is the information from the readme.
Convert a number, represented as a sequence of digits in one base,...
4
votes
1
answer
524
views
Solving a linear system with two variables
Background:
Equation 1: 2x-y=6
Equation 2: 4x+3y=22
Because 1 is ax-by=c and 2 is dx-ey=f
y=(a * f - c * d) / (a * e - b * d) and x is simply (c - (b * y)) / a)
What I am trying to do:
...
3
votes
4
answers
8k
views
C# Code to Find all Divisors of an Integer
I made this so I can feed it integers and return an array with all the divisors of that integer. I put checks in in case the integer is less than 2. Order of integers in the array must be smallest ...
3
votes
2
answers
470
views
Get resulting rectangle from two overlapping rectangles
I have a function that creates a rectangle from two rectangles that overlap, I was wondering if there is a smarter way to do it purely mathematically rather than lots of ...
3
votes
2
answers
126
views
Project price of publicly traded contract
I have to project the price for a publicly traded contract when the price should be approximately matching the targeted profit or loss. I have written the following code, runs under 0.02ms.. could be ...
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
<...
8
votes
1
answer
691
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
2
answers
968
views
Find coprime numbers less than n
I get the coprime numbers less than n with the following algorithm:
...
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 ...
6
votes
4
answers
612
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 ...
4
votes
1
answer
645
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
211
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 =...
3
votes
5
answers
2k
views
Absolute Values and Fractions
$$
\frac{|x|-|y|}{1+|xy|}
$$
How can I reduce the amount of code without losing quality? What are your tips? Your comments on the structure, logic, in general, everything. How to make the type of ...
-2
votes
3
answers
180
views
Find target sum of 3 from and array
Input is a sorted array and a target sum. Find all sets of 3 unique elements that sum to a target.
Looking for lowest order (big O).
A, B, C are the indexes on the sorted array. A < B < C
...
7
votes
2
answers
861
views
Optimizing special cases of modulo
I have a use case where improvements to the speed of calculating modulo for 64 bit integer dividends makes a significant difference in overall processing time.
The divisor isn't known statically - it ...
2
votes
2
answers
672
views
Convert a double to a string containing rational and surd
First off, code:
...
8
votes
1
answer
3k
views
Generating Uniformly Distributed Random Numbers
Edit: Dunno what PCG is and don't want to read the paper? Maybe this video of Melissa O'Neill (the author) explaining things will be palatable instead.
Original: I attempted to answer this question ...
10
votes
2
answers
248
views
Resizing a discrete uniform CSRNG distribution
I have a requirement to generate a uniform distribution of cryptographically secure random numbers. To generate these numbers I only know of the ...
4
votes
2
answers
4k
views
Calculating distance and angle between two points, C# program
I like to think I'm an intermediate programmer, but I learned to code with the Unity engine so I'm still getting used to not using it, and I've never had someone review my code before. I don't know ...
7
votes
2
answers
182
views
Sum of the reciprocals of whole numbers adding to one
I have written some code which works out which sums of reciprocals of whole numbers sum to one, e.g:
\$\frac 12 +\frac 13 +\frac 16 =1 \$
\$\frac 17 +\frac 18 +\frac 19 + \frac1{10} + \frac1{11}+ \...
7
votes
2
answers
1k
views
Squaring the two digits in the middle of a four-digit number
I got a four-digit positive number. I need to square the two digits in middle to generate a new number on which the same procedure is applied. The numbers get saved in a list. This loops as long as ...
3
votes
1
answer
5k
views
Probability Chance
I'm creating a game and I need some properties to work as percent chance to hit/dodge. I implemented a small testing program that takes percent chance and the amount of hitting attempts as input and ...
5
votes
2
answers
11k
views
Add item after every nth step
The purpose of the method is to add a list of items after every nth step (5 for example) where the input items are repeated if needed. I'm looking for an overall review and possible how to remove the ...
3
votes
1
answer
10k
views
Calculating distance with Euclidean, Manhattan, and Chebyshev in C#
I am by no means proficient at being able to take mathematical concepts and converting them to code. I was wondering if you guys could look at it and make suggestions on how I could fix it or if my ...
8
votes
1
answer
3k
views
Faster(?) factorial calculations
I saw this question, but was unable to answer because of my inexistant knowledge of Rust, so I decided to try and write an algorithm on my own and put it for review.
There is the casual way to ...
5
votes
1
answer
12k
views
Better implementation of Gaussian Elimination
I made an algorithm in C# that solves any system of linear equations using the Gaussian elimination. There are 2 text boxes in the program for input and output. Input is in the format of the ...
1
vote
1
answer
80
views
A banded interpolator, for any type and any interpolation method
This is an extension of my Attributes system I wrote about in these questions:
Lvl 1 upgradeable attributes
Lvl2 upgradeable attributes
It is unrelated to the core functionality under review in ...
4
votes
2
answers
5k
views
Square Root Calculator
I have now written a simple square root calculator using the division method:
...
5
votes
4
answers
2k
views
Project Euler #5 - Lowest common multiple of 1 through 20
This is my code. All comments welcome. Last time run it only took 335 milliseconds:
...
10
votes
8
answers
13k
views
Calculate square of a number without multiplication
I wrote these two methods to calculate the square of a number:
...
5
votes
3
answers
2k
views
Square root implementations
The question was:
Write a function that calculates the root of a given number.
Answer A = I wrote it for integers. just run up to half of the number
Answer B+C = are for double, using Newton's law....
6
votes
1
answer
336
views
Immutable Matrix
I'm writing implementations of some numerical methods to solve linear equations systems, those implementations use the following Matrix class. I'm trying to get this class immutable, due to that the ...
4
votes
1
answer
796
views
Project Euler #12 very long runtime
Project Euler Problem 12 asks to find the value of the first triangular number to have over 500 divisors, where the \$n\$th triangular number is \$\sum_{i=1}^{n} i\$.
The sequence of triangle ...
10
votes
4
answers
1k
views
Solving for the zero of an arbitrary function
This methods solves for the zero of an arbitrary function using the Bisection Method. It works as desired, but I want to know if the code can be improved or made more readable and if I'm properly ...
15
votes
3
answers
2k
views
Wow that's a big integer! What's its largest prime factor?
I see that there are a few other people who have tackled Project Euler Problem #3. I hope you're not all sick of that question yet. I've not taken a look at those yet (purposely), but am about to now. ...
12
votes
4
answers
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 ...
4
votes
1
answer
7k
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 ...
1
vote
1
answer
497
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 <...
1
vote
1
answer
104
views
Is this routine for determining the epsilon of a Double 100% valid?
This algorithm is intended to determine the epsilon of a given double precision number. As numbers increase in value, their accuracy decreases. This algorithm will return the smallest increment / ...
2
votes
1
answer
4k
views
Generating Pandigital Numbers
A Base10 Pandigital Number is a number which uses all the digits 0-9 once:
1234567890
2468013579
etc...
My naive solution was just to use a bunch of nested loops to do this but it's quite slow. And ...