Q1: Write a method, pow, that takes two (non-negative, integer) numbers, base and exponent and returns base raised to the exponent power. (No fair using Ruby's base ** exponent notation!).
Q2: Write a method, sum which takes an array of numbers and returns the sum of the numbers.
Q3: Write a method, is_prime?, that takes a number num and returns true if it is prime and false otherwise.
You may wish to use the modulo operation: 5 % 2 returns the remainder when dividing 5 by 2: 1. If num is divisible by i, then num % i == 0. (You would not be expected to already know about modulo for the challenge)
Q4: Using your is_prime? method, write a new method, primes that takes a (non-negative, integer) number max and returns an array of all prime numbers less than max.
I am interested in the simplest solutions. If you could include step by step explanations that would be great.

3
Contributors
4
Replies
32
Views
4 Years
Discussion Span
Last Post by Herve_1

I am interested in the simplest solutions. If you could include step by step explanations that would be great.

@Herve_1

My question is how did you came up with these questions.

It's more related to school.

What book do you have? It should have an examle of that.

Hi,

There is no simplest form of a solution to a problem, without going into the basics.

I never crossed past the PHP forum, but I just did this time. It is not that I am not capable of writing codes in Ruby, but it is because I am so darn lazy.

My question is, what good will it brings you, if I or someone ever give you the codes that will satisfy your school homework questions?

Here are the stages of Ruby that I have to go through, while learning the language. Believe me it is not that hard really... at least for me. I just have to remind me about all these things.

1. Learn the form formatter.

first_name = gets.chomp

2. Learn the operators like so..

Subtraction (-)
Multiplication (*)
Division (/)
Exponentiation (**)
Modulo (%)

to multiply

``````1 * 2
``````

``````1 + 2
``````

to exponentiate

``````2 ** 2
``````
1. Learn sring function . Remember pretty much everything in Ruby are objects.

to get the length of a string

``````"mystring".length

"mystring".upcase

# this will create a  lower case of MYSTRING
"MYSTRING".downcase
``````
1. Learn the naming conventions

my_localvariable = "hello Ruby"

# this is legal in Ruby, but you must stay within the norms

@my_localvariables = "hello Ruby"

2. Master the control flow as you move along

print "Type a number: "
num_typed = Integer(gets.chomp)

if num_typed > 0
puts "you just typed an positive integer"

elseif num_type < 0
puts "you just type a negative integer"

else

puts "it is nada, zip, zero"

end

3. Learn what is unique in Ruby than other languages.. for example unless.

submitted = false

unless submitted
puts " I ain't gonna let you go"

4. Learn comparison

think_twice = 6 != 20

# less than or equal to something

this_integer = 3 <= 5

5. Know your Boolean operators. This is pretty important in programming. OR, AND, NOT

|| && !

example will return an boolean true

give_boolean = 52 == 25 && 42 == 16

1. Learn all the loops while, until, for. Learn iterators block

2. Learn Methods , Sorting and Blocks. This applies to your questions

def makeCube(n)
puts n ** 3
end

makeCube(4)

6. Learn how to write Object Oriented in Ruby

Bonus: One of your question has been answered.. what a luck huh?

``````def determine(numberX)

puts "Not an Integer." unless n.is_a? Integer

is_prime = true
for x in 2..numberX-1

if numberX % x == 0
is_prime = false

end
end

if is_prime
puts "#{numberX} yes, it is a prime number!"
else
puts "#{numberX} is not a prime number."
end
end
``````

to test the function above... try ..

``````prime(51)
prime(11)
``````

that should give you an output of : 51 is not a prime number, and 11 yes, it is a prime number!.

change these

``````    prime(51)
prime(11)
``````

to

``````    determine(51)
determine(11)
``````