Skip to content

Instantly share code, notes, and snippets.

View deniskyashif's full-sized avatar

Denis Kyashif deniskyashif

View GitHub Profile
(function() {
'use strict';
Function.prototype.inherits = function(Parent) {
this.prototype = new Parent();
this.prototype.constructor = this;
};
var Person = function (name) {
this.name = name;
x * 2^y = x << y
x / 2^y = x >> y
x % y = x & (y-1)
@deniskyashif
deniskyashif / RSelect.cs
Created July 26, 2015 07:31
randomized selection in C#
using System;
using System.Collections.Generic;
public class RSelect<T> where T : IComparable {
private Random rnd = new Random();
public T Select(IList<T> collection, int left, int right, int k) {
if((right - left) <= 1)
return collection[right];
using System;
class Program
{
private const ulong N = 600851475143;
static void Main()
{
bool[] composites = new bool[(ulong)Math.Sqrt(N)];
ulong length = (ulong)composites.Length;
/* The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even) n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence: 134020105168421
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
/*
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
*/
using System;
class Program
{
/*
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19
letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115
(one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
*/
// Find the maximum total from top to bottom of the triangle below:
using System;
class Program
{
static void Main()
{
var triangle = new int[][]
{
var validator = $('#form').kendoValidator({
messages: {
text: 'Field must contain at most 20 symbols.',
grid: 'The grid must contain at least two rows.'
},
rules: {
text: function(item) {
if (item.is('[name=text]')) {
return item.val().length < 20;
}
/*
The problem: define functions range, map, reverse and foreach, obeying the restrictions below, such that the following program works properly.
It prints the squares of numbers from 1 to 10, in reverse order.
You must not use arrays. The square bracket characters, [ and ], are forbidden, as well as new Array.
You must not use objects. The curly braces, { and }, and the dot character (.) are forbidden. You may use curly braces for code blocks, but not for creating JavaScript objects.
Should go without saying, these functions must be generic and do what their name implies. They must not be hard-coded for the particular 1..10 example.
*/