Giter Club home page Giter Club logo

sieve-eroatosthness's Introduction

sieve-eroatosthness

The sieve Of Eratosthness It is an ancient algorithm to calculate the prime numbers. The algorithm is as follows:

  1. Start with all the integers starting from 2, the very first prime number.
  2. Eliminate all the multiples of 2.
  3. The first element of the resulting list is 3, which is the immediate prime number.
  4. Eliminate all the multiples of 3.
  5. Iterate forever. At each iteration, the first number in the list is a prime number and we eliminate all the multiples of the number.

How do we do this in Scala?

The key idea is to eliminate the multiples, this could be done in Scala using 'filter' And, lets do this for all the natural numbers.

Lazy evaluation in streams in scala opens gates for the infinite streams.

In scala, all the elements of a stream except the first one are computed only when they are needed. This opened the possibility to definite infinite streams. Ex: Stream of all integers starting from a given number is simple.

def from(n: Int): Stream[Int] = n #:: from(n+1)

In the above code, from(n+1) is calculated only when needed. <- Lazy evaluation.

We could generate a stream of all the natural numbers: from(0)

Stream of all multiples of 72: from(1).filter(_ * 72)

sieve-eroatosthness's People

Contributors

mlidal avatar msravan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.