KJWBeige / ruby_challenges

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#Ruby Code challenges!!

  1. Fork this repo

  2. Clone it.

  3. Write your code in the quiz.rb file.

  4. Once you think you have it commit, push and send me a pull request

In these puzzles, we are building a function which finds prime numbers! This is a good example of a challenge which may be asked of you at a coding interview. To find prime numbers, we break the problem into two steps:

  • Given a number, find all of its divisors.
  • If a number only has two divisors, it is prime!

##Divisors

###Objective

Given an integer, output an array of its divisors.

Your method is called divisor

###Divisor

A divisor of a number evenly divides it. For example:

3 is a divisor of 6, because 6 % 3 == 0.
10 is a divisor of 100, because 100 % 10 == 0.
4 is a divisor of 4, because 4 % 4 == 0.

2 is not a divisor of 3, because 3 % 2 == 1.

###Input

An integer n, such that 0 < n < Inf.

###Ouput

An array of the divisors of the input integer.

###Examples

Input: 7
Output: [1, 7]

Input: 27
Output: [1, 3, 9, 27]

Input: 30
Output: [1, 2, 3, 5, 6, 10, 15, 30]

##Primes

###Objective

Given an integer, return an array of prime numbers up to that integer. Use the divisor function written in Divisors.

your method is called prime

###Prime Number

A prime number is a number which is divisible by only two numbers -- 1 and itself. The number 1 is divisible by only one number, 1, so it is not prime. The first ten prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29

###Input

A positive integer n, such that 0 < n < Inf

###Output

An array of all prime numbers up to and including the input integer.

###Examples

Input: 7
Output: [2, 3, 5, 7]

Input: 48
Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

###Fibonacci Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

your mission, if you choose to accept it is to write a method fib that takes an integer and returns an array of the fibonacci sequence up to that number

###Input

A positive integer n, such that 0 < n < Inf

###Output

An array of the fibonacci sequence up to and including the input number ordered from least to greatest.

###Examples

Input: 13  
Output: [1,1,2,3,5,8,13]

Input: 73
Output: [1,1,2,3,5,8,13,21,34,55]

About


Languages

Language:Ruby 100.0%