This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def FindLargestPrime(n) | |
| return 1 if n == 1 | |
| prime_factor = 2 | |
| while prime_factor * prime_factor <= n do | |
| if n % prime_factor == 0 then n /= prime_factor | |
| else prime_factor += 1 | |
| end | |
| end | |
| n | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fib1 = 1 | |
| fib2 = 2 | |
| sum = fib2 | |
| fib = 0 | |
| until (fib >= 4000000) | |
| fib = fib1 + fib2 | |
| fib1 = fib2 | |
| fib2 = fib | |
| sum += fib if fib.even? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Project Euler 1sum = 0 | |
| (1...1000).each do |num| | |
| sum += num if num % 3 == 0 || num % 5 == 0 | |
| end | |
| puts sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def palindrome?(a) | |
| a.to_s == a.to_s.reverse | |
| end | |
| def dev_by_3digit?(a) | |
| 999.downto(100) do |i| | |
| if a % i == 0 && a / i < 999 | |
| return true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def devisible?(num, limit) | |
| devisible = true | |
| limit.downto(1) do |i| | |
| devisible = false if num % i != 0 | |
| break if !devisible | |
| end | |
| devisible | |
| end | |
| def smallest_multiple(limit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sum_of_squares(num) | |
| (1..num).inject { |mem, var| mem + var ** 2 } | |
| end | |
| def square_of_sum(num) | |
| sum = (1..num).inject { |mem, var| mem + var } | |
| sum ** 2 | |
| end | |
| p square_of_sum(100) - sum_of_squares(100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sum of first n numbers is n(n+1)/2 | |
| # Sum of squares of first n numbers is n(n+1)(2n + 1)/6 | |
| # Expanded form for sum ^ 2 - sum of squares = n^4/4+n^3/6-n^2/4-n/6 | |
| def formula(n) | |
| (n ** 4) / 4 + (n ** 3) / 6 - (n ** 2) / 4 - n / 6 | |
| end | |
| p formula(100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| primes = Prime.first 10001 | |
| p primes.last |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| ivan = Student.new 'Иван', 10, :second | |
| mariya = Student.new 'Мария', 12, :first | |
| neycho = Student.new 'Нейчо', 9, :third | |
| students = [ivan, mariya, neycho] | |
| describe "Array#to_proc" do | |
| it "works for a single element" do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __foo__ = "This foo sentence contains many foo instances, for example: foo, foo, foo." | |
| * Match a single symbol: `foo.match /./ # => #<MatchData "T">` | |
| * Match the first-found word character symbol: `foo.match /\w/ => #<MatchData "T">` | |
| * Extract all symbols of a string: `foo.scan /./ # => ["T", | |
| "h", | |
| "i", | |
| "s", | |
| " ", | |
| "f", |
OlderNewer