Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad
Mathematics
Algorithms
Logic
Numbers

Multiplication of Complex Numbers

Related Collection

Complex Analysis - click the link to try it out now :D

The Problem

Given two complex numbers z = x + iy and w = u + iv, how do I multiply them together (i.e. compute z * w)?

The Solution

First, use the FOIL rule (first, outer, inner, last) to multiply and expand the expression:

z * w = (x + iy) * (u + iv) = xu + xiv + iyu + yvi^2

Then note that i^2 = -1 by definition and collect real and imaginary terms:

xu + xiv + iyu + yvi^2 = xu + i(xv + yu) - yv = (xu - yv) + i(xv + yu)
function multiply(z, w) {
  var x = Re(z);
  var y = Im(z);
  var u = Re(w);
  var v = Im(w);
  return new ComplexNumber(x * u - y * v, x * v + y * u);
}

Calculate A = B + alpha * C

proc stream(b, c, alpha) {
    return b + alpha * c;
}
Mathematics
Algorithms
Logic
Numbers

Division of Complex Numbers

Related Collection

Complex Analysis <-- click on the link to train on this Collection :D

The Problem

Given two complex numbers z = x + iy and w = u + iv, how do I divide one by the other, e.g. compute z / w (or the reverse)?

The Solution

Just as one may rationalize the denominator of 1 / sqrt(2) to obtain sqrt(2) / 2, it is also possible to real-ize the denominator of z / w by multiplying both the numerator and denominator by the complex conjugate of "w" w* = u - iv. Then use the identity i^2 = -1 where necessary and collect real and imaginary terms.

z / w
= (z * w*) / (w * w*)
= ((x + iy) * (u - iv)) / ((u + iv) * (u - iv))
= (xu - xiv + iyu - yvi^2) / (u^2 - (iv)^2)
= ((xu + yv) + i(yu - xv)) / (u^2 + v^2)
= ((xu + yv) / (u^2 + v^2)) + i((yu - xv) / (u^2 + v^2))

Also note that u^2 + v^2 = |w|^2 which may help to simplify your code further.

function divide(z, w) {
  var x = Re(z);
  var y = Im(z);
  var u = Re(w);
  var v = Im(w);
  const abs = z => Math.hypot(Re(z), Im(z));
  return new ComplexNumber((x * u + y * v) / Math.pow(abs(w), 2), (y * u - x * v) / Math.pow(abs(w), 2));
}
Mathematics
Algorithms
Logic
Numbers

Computing the real Gamma Function with Stirling's Approximation

Related Kata

Computing the complex Gamma function <-- click on the link to attempt the Kata now :D

Related Collection

Complex Analysis

Overview

The Gamma Function Γ(x) is an extension of the factorial function - while the factorial n! is only defined for non-negative integers, the gamma function is defined for all numbers except the non-positive integers. However, the gamma function has its argument shifted down by 1 such that Γ(n) = (n - 1)! for all n where n is a positive integer. One of its many applications is in fractional calculus.

Definitions

The main definition of the gamma function is based on a definite integral with positive infinity as one of its limits. There are also other exact definitions of the gamma function such as "Euler's definition as an infinite product" and the "Weierstrass definition". However, I will not elaborate on these definitions - more information can be easily found on Wikipedia (or by consulting your math professor).

The Problem

It is technically impossible to implement an exact definition of the Gamma Function in a computer/calculator since "there are, relatively speaking, no such simple solutions for factorials; no finite combination of sums, products, powers, exponential functions, or logarithms will suffice to express x!" (source: Wikipedia) so one must always resort to numerically approximating the Gamma function, ideally to a high degree of accuracy. A common, well-known approximation to the Gamma Function is known as Stirling's Approximation which has a simple formula and is usually sufficiently accurate for large values of x; however, since it is an asymptotic approximation, it loses its accuracy for small values of x and doesn't work with negative values of x due to an attempt at squarerooting a negative number (JavaScript Math.sqrt returns NaN for negative inputs).

The Challenge

Stirling's Approximation is implemented in this Kumite as a possible implementation for the Gamma Function; however, you will notice that it fails most, if not all, of the tests. The challenge, should you accept it, is to properly implement the Gamma Function such that it passes all test cases properly.

function gamma(x) {
  // Stirling's Approximation is simple and efficient (just a single calculation)
  // but will it work?
  x -= 1; // Shift argument down by 1
  return Math.sqrt(2 * Math.PI * x) * Math.pow(x / Math.E, x); // Compute Stirling's Formula
}

Game of Life :

The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.

Game of Life

Your task is to write a program to calculate the next generation of Conway's game of life, given any starting position.
You start with a two dimensional grid of cells, where each cell is either alive or dead.
The grid is finite, and no life can exist off the edges.
When calculating the next generation of the grid, follow these four rules:

  1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
  2. Any live cell with more than three live neighbours dies, as if by overcrowding.
  3. Any live cell with two or three live neighbours lives on to the next generation.
  4. Any dead cell with exactly three live neighbours becomes a live cell.

Examples: * indicates live cell, . indicates dead cell

Example input: (4 x 8 grid)
........
....*...
...**...
........

Example output:
........
...**...
...**...
........
function nextGeneration(grid) {
  return grid.map((row, rowIndex) => {
    return row.map((cell, colIndex) => {
      if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
        let neighboursCount = 0;
        if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
        if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
        if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
        if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
        if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
        if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
        if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
        if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
      
        if (cell === 1) {
          if (neighboursCount === 2 || neighboursCount === 3 ) {
            return 1;
          }
        } else {
          if (neighboursCount === 3 ) {
            return 1;
          }
        }
        return 0;
      }
      return 0;
    });
  });
}
Puzzles
Games
Mathematics
Algorithms
Logic
Numbers
Data Types
Sequences
Arrays

Description

The Kolakoski Sequence is a infinite string of integers that describes it own construction. Each digit, in order, encodes the size of the run that some digit in the sequence appears.

The inital terms of the sequence seeded by [1,2] are:

1, 2, 2, 1, 1, 2, 1, 2, 2...

Notice that there is one 1, followed by two 2s, then two 1s. In general the repitition of digits can be described as:

1, 2, 2, 1, 1, 2, 1, 2, 2...

Or the sequence itself. That is the idea you will be exploring in this excercise.

Instructions

Task 1:

Generate_Kolakoski_Seq(seed, n)
Write a function that takes an array of symbols as a seed, and an integer n, and produces the Kolakoski Sequence for that seed up to the nth digit as a common separted string.

Generate_Kolakoski_Seq( [2,3], 11 )

returns "2,2,3,3,2,2,2,3,3,3,2"

Generate_Kolakoski_Seq( [2], 5 )

returns "2,2,2,2,2"

Task 2:

Find_Kolaskoski_Number(seed, n)
Write a function that takes an array of symbols and a large integer n, and returns the nth digit of the sequence as an integer.

Find_Kolaskoski_Number( [1,2], 10000 )

returns: 1

(There is a way to compute this without computing the whole sequence)

// takes int[] and int, returns string (comma seperated integers)
function Generate_Kolakoski_Seq(seed, n){

}


// takes int[] and int, returns int
function Find_Kolaskoski_Number(seed, n){

}
Arrays
Data Types

Function will recive N for length of array and point as index to pint at in that array.


For example, function should generate arrays like this for


N = 10, point = 7
[7,6,5,4,3,2,1,0,1,2]


N = 10, point = 4
[4,3,2,1,0,1,2,3,4,5]

function func(N, point) {
  let start = 0; // starting position of array
  let clonePoint = point; // clone for point to start counting from that number at begining of array
  let arr = [...Array(N).keys()] // generate array and fill with 0 to 10
  if(!(point > N)) {
    arr.forEach((o, index) => {
      index < point ? arr[index] = clonePoint-- : arr[index] = start++;
    });
    return arr;
  }
  return [];
}
use Math;

proc chebyshev(n: int, v: real) : real
{
  if (v > 1) {
    return cosh(n * acosh(v));
  } else if (v < -1) {
    return (-1) ** n * cosh(n * acosh(-v));
  } else {
    return cos(n * acos(v));
  }
}

Hello World in Julia, since there's nothing on codewars ⊙﹏⊙.

println("Hello Julia!")
const hello = "world"
// add the values "codewars" to the websites array
var websites = ['codears'];