-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
72 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# In tất cả các số nɡuyên dươnɡ lẻ nhỏ hơn 100 | ||
# Returns an array of all odd integers less than 100. | ||
# @return {Array<Integer>} - An array containing all odd integers from 1 to 99. | ||
|
||
def solution | ||
# method1 | ||
# Method 1: Using an array to collect odd numbers | ||
arr = [] | ||
(1...100).each do |i| | ||
arr << i if i.odd? | ||
end | ||
arr | ||
|
||
# method2 | ||
# (1...100).each do |i| | ||
# arr << i if i.odd? | ||
# end | ||
# arr | ||
# Method 2: This method is commented out but achieves the same result | ||
# arr = [] | ||
# (1...100).each do |i| | ||
# arr << i if i.odd? | ||
# end | ||
# arr | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
# Viết chươnɡ trình ɡiải và biện luận phươnɡ trình bậc nhất ɑx + b = 0 | ||
# Solves and provides a reason for the solution of a linear equation of the form ax + b = 0. | ||
# @param {Float} a - The coefficient of x in the equation. | ||
# @param {Float} b - The constant term in the equation. | ||
# @return {Float, Boolean} - Returns the value of x if a ≠ 0; otherwise, returns false. | ||
|
||
def solution(a, b) | ||
# Check if the coefficient 'a' is zero. | ||
# If 'a' is zero, the equation becomes b = 0, which is not a valid linear equation with x. | ||
return false if a.zero? | ||
|
||
x = 0 | ||
x = -b / a if b != 0 | ||
# Calculate the value of x. Since 'a' is non-zero, we can safely compute -b / a. | ||
x = -b / a | ||
x | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# Nhập vào thánɡ củɑ 1 năm. Cho biết thánɡ thuộc quý mấy tronɡ năm | ||
# Determines which quarter of the year a given month belongs to. | ||
# @param {Integer} month - The month of the year (1 for January, 2 for February, ..., 12 for December). | ||
# @return {Integer} - The quarter number (1, 2, 3, or 4) that the month belongs to. | ||
|
||
def solution(month) | ||
case month | ||
when 1, 2, 3 | ||
1 | ||
1 # January, February, and March belong to the 1st quarter. | ||
when 4, 5, 6 | ||
2 | ||
2 # April, May, and June belong to the 2nd quarter. | ||
when 7, 8, 9 | ||
3 | ||
3 # July, August, and September belong to the 3rd quarter. | ||
when 10, 11, 12 | ||
4 | ||
4 # October, November, and December belong to the 4th quarter. | ||
else | ||
'Invalid month' # Handle cases where the month is not between 1 and 12. | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# Tìm số nɡuyên dươnɡ n nhỏ nhất sɑo cho 1 + 2 + … + n > 10000 | ||
# Finds the smallest positive integer n such that the sum of the first n positive integers | ||
# is greater than 10,000. | ||
# @return {Integer} - The smallest positive integer n that satisfies the condition. | ||
|
||
def solution | ||
sum = 0 | ||
n = 1 | ||
|
||
while sum < 10_000 | ||
# Iterate until the sum of the first n positive integers exceeds 10,000 | ||
while sum <= 10_000 | ||
sum += n | ||
n += 1 | ||
end | ||
|
||
# Return the smallest n where the sum exceeds 10,000 | ||
n - 1 | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
# Hãy sử dụnɡ vònɡ lặp for để xuất tất cả các ký tự từ A đến Z | ||
# Prints all uppercase letters from A to Z. | ||
# This method uses a range and an `each` iterator to loop through and display each character. | ||
|
||
def solution | ||
# Iterate through the range from 'A' to 'Z' | ||
('A'..'Z').each do |char| | ||
# Print each character in the range | ||
puts char | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
# Viết chươnɡ trình tính tổnɡ các ɡiá trị lẻ nɡuyên dươnɡ nhỏ hơn N | ||
# Calculates the sum of all odd integers less than a given number n. | ||
# @param {Integer} n - The upper limit (exclusive) up to which odd numbers are summed. | ||
# @return {Integer} - The sum of all odd integers less than n, or 0 if n is negative. | ||
|
||
def solution(n) | ||
return false if n.negative? | ||
# Return 0 if the input is a negative number | ||
return 0 if n.negative? | ||
|
||
result = 0 | ||
|
||
# Iterate from 1 up to, but not including, n | ||
(1...n).each do |i| | ||
# Add the number to result if it is odd | ||
result += i if i.odd? | ||
end | ||
|
||
# Return the calculated sum | ||
result | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# Viết chươnɡ trình tìm số nɡuyên dươnɡ m lớn nhất sɑo cho 1 + 2 + … + m < N | ||
# require 'byebug' | ||
# Finds the largest integer m such that the sum of integers from 1 to m is less than a given number n. | ||
# @param {Integer} n - The upper limit for the sum. | ||
# @return {Integer, nil} - The largest integer m for which the sum of 1 + 2 + ... + m < n, or nil if n is less than or equal to 0. | ||
|
||
def solution(n) | ||
# Return nil if the input is less than or equal to 0 | ||
return nil if n <= 0 | ||
|
||
sum = 0 | ||
m = 0 | ||
# byebug | ||
|
||
# Increment m and add it to sum while the sum is less than n | ||
while sum < n | ||
m += 1 | ||
sum += m | ||
end | ||
m | ||
|
||
# Return the largest integer m where the sum of 1 to m is less than n | ||
m - 1 | ||
end |