Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / IsPrimeNumber.cs
Last active August 29, 2015 14:12
Checks if a number is a prime number
public bool IsPrime(int number)
{
return !Enumerable.Range(2, (int)Math.Sqrt(number) - 1).Any(x => number % x == 0);
}
@vlad-bezden
vlad-bezden / times.js
Last active August 29, 2015 14:17
repeat string by number of times in JavaScript
String.prototype.times = function(count) {
return count < 1 ? '' : new Array(count + 1).join(this);
};
@vlad-bezden
vlad-bezden / FirstDivisor.cs
Last active August 29, 2015 14:20
Finds first divisor of any given number
public int FirstDivisor(int number)
{
return Enumerable.Range(2, (int)Math.Sqrt(number)).FirstOrDefault(x => number % x == 0);
}
@vlad-bezden
vlad-bezden / ProducerConsumer.cs
Created May 22, 2015 14:54
Example of Producer/Consumer using BlockingCollection
private readonly BlockingCollection<int> _blockingQueue = new BlockingCollection<int>();
void Main()
{
var producer = Producer();
var consumer = Consumer();
Task.WaitAll(producer, consumer);
Console.WriteLine("Done");
@vlad-bezden
vlad-bezden / ProducerConsumerAsync.cs
Created May 22, 2015 18:45
Producer/Consumer example using asynchronous blocking queue. This example has 10 producers and 1 consumer. BufferBlock<T> is in the Microsoft.Tpl.Dataflow NuGet package
private BufferBlock<int> queue = new BufferBlock<int>();
void Main()
{
var producers = Enumerable.Range(0, 10).Select(async x =>
{
await Producer();
});
var consumer = Consumer();
@vlad-bezden
vlad-bezden / GCD.cs
Last active December 6, 2015 13:12
Euclidean Greatest Common Divisor (GCD) algorithm
public int GCD(int first, int second)
{
return second == 0
? first
: GCD (second, first % second);
}
@vlad-bezden
vlad-bezden / fibonacci.fs
Created December 6, 2015 13:14
Fibonacci implementation in F#
let rec fibonacci n =
match n with
| 1 | 2 -> 1
| _ -> fibonacci (n - 1) + fibonacci (n - 2)
@vlad-bezden
vlad-bezden / Factorial.fs
Last active December 6, 2015 13:16
Factorial implementation in F# using reduce
let factorial n =
[1..n] |> List.reduce (*)
@vlad-bezden
vlad-bezden / Factorial.fs
Created December 6, 2015 13:17
Factorial implementation in F# using pattern matching
let rec factorial n =
match n with
| 0 | 1 -> 1
| _ -> n * factorial(n - 1)
@vlad-bezden
vlad-bezden / TowerOfHanoi.fs
Last active September 22, 2020 08:13
Solution of Tower of Hanoi problem using F#
let rec TowerOfHanoi fromPole destPole tempPole disks =
if disks > 0 then
TowerOfHanoi fromPole tempPole destPole (disks - 1)
printfn "Moving from %c to %c" fromPole destPole
TowerOfHanoi tempPole destPole fromPole (disks - 1)
TowerOfHanoi '1' '2' '3' '4'