Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
Ruby Tutorial
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tuấn Nguyễn Ngọc Minh
Ruby Tutorial
Commits
a8bc4ed7
Commit
a8bc4ed7
authored
Jul 17, 2014
by
Tuấn Nguyễn Ngọc Minh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
This is the first submit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
258 additions
and
0 deletions
+258
-0
input.txt
+2
-0
submit.rb
+256
-0
No files found.
input.txt
0 → 100644
View file @
a8bc4ed7
7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
\ No newline at end of file
submit.rb
0 → 100644
View file @
a8bc4ed7
#Problem 1
def
problem1
result
=
0
i
=
0
;
while
(
i
<
1000
)
do
if
(
i
%
3
==
0
||
i
%
5
==
0
)
then
result
+=
i
end
i
+=
1
end
puts
result
end
problem1
#Problem 2
def
problem2
(
nth
)
number1
=
0
number2
=
1
result
=
0
while
(
number2
<
nth
)
do
if
(
number2
.
even?
)
then
result
+=
number2
end
temp
=
number1
+
number2
number1
=
number2
number2
=
temp
end
puts
result
end
problem2
(
4_000_000
)
#Problem 3
def
probelm3
(
arg
)
nth
=
arg
x
=
3
result
=
-
1
while
(
Math
.
sqrt
(
x
)
<
nth
)
do
res
,
rem
=
nth
.
divmod
(
x
)
if
(
rem
==
0
)
then
nth
=
nth
/
x
result
=
x
x
+=
2
else
x
+=
2
end
end
return
result
end
puts
probelm3
(
600_851_475_143
)
#Problem 4
def
palindrome
(
arg
)
temp
=
arg
.
to_s
return
temp
.
reverse
==
temp
end
def
problem4
result
=
0
for
x
in
999
.
downto
(
100
)
for
y
in
999
.
downto
(
100
)
temp
=
x
*
y
if
(
palindrome
(
temp
))
then
if
(
temp
>
result
)
then
result
=
temp
end
end
end
end
return
result
end
puts
problem4
#Problem 5
def
problem5
set
=
(
1
..
20
).
to_a
return
set
.
inject
(
:lcm
)
end
puts
problem5
#Problem 6
def
problem6
sumOfSquares
=
0
squareOfSum
=
0
for
x
in
100
.
downto
(
0
)
sumOfSquares
+=
x
*
x
squareOfSum
+=
x
end
squareOfSum
=
squareOfSum
*
squareOfSum
return
squareOfSum
-
sumOfSquares
end
puts
problem6
#Problem 7
def
is_prime?
(
num
)
Math
.
sqrt
(
num
).
floor
.
downto
(
2
).
each
{
|
i
|
return
false
if
num
%
i
==
0
}
return
true
end
def
problem7
(
nth
)
number
=
0
;
currentPrime
=
2
;
while
(
true
)
do
if
(
is_prime?
(
currentPrime
))
then
number
+=
1
if
(
number
==
nth
)
then
return
currentPrime
end
end
currentPrime
+=
1
end
end
puts
problem7
(
10_001
)
#Problem 8
def
problem8
(
link
)
File
.
open
(
link
).
each_line
{
|
s
|
array_number
=
s
.
to_s
.
split
(
''
)
for
i
in
0
..
999
array_number
[
i
]
=
array_number
[
i
].
to_i
end
result
=
0
for
i
in
0
..
987
temp
=
array_number
[
i
]
for
x
in
1
..
12
temp
*=
array_number
[
i
+
x
]
end
if
(
temp
>
result
)
then
result
=
temp
end
end
return
result
}
end
puts
problem8
(
'input.txt'
)
#Problem 9
def
problem9
for
a
in
1
..
1000
for
b
in
1
..
1000
c
=
1000
-
a
-
b
if
(
Math
.
hypot
(
a
,
b
)
==
c
)
then
return
a
*
b
*
c
;
end
end
end
return
-
1
;
end
puts
problem9
# Problem 10
# Cach thu 1: thoi gian tinh toan khoang 3'
def
is_prime?
(
num
)
Math
.
sqrt
(
num
).
floor
.
downto
(
2
).
each
{
|
i
|
return
false
if
num
%
i
==
0
}
return
true
end
def
problem10
(
nth
)
result
=
0
for
i
in
2
..
nth
if
(
is_prime?
(
i
))
then
result
+=
i
end
end
return
result
end
puts
problem10
(
2_000_000
)
# Cach thu 2: giam kich thuoc tinh toan, bang cach loai bo nhung so chan (even), thoi gian tinh toan giam xuong 1 nua, khoang gan 2' (Eliminating even numbers)
def
is_prime?
(
num
)
Math
.
sqrt
(
num
).
floor
.
downto
(
2
).
each
{
|
i
|
return
false
if
num
%
i
==
0
}
return
true
end
def
problem10
(
nth
)
result
=
0
for
i
in
2
..
nth
unless
(
i
.
even?
)
then
if
(
is_prime?
(
i
))
then
result
+=
i
end
end
end
return
result
end
puts
problem10
(
2_000_000
)
# Cach thu 3: toi uu nhat, thoi gian tinh toan khoang 1-2s @@ ap dung giai thuat Sieve of Eratosthenes
def
problem10
(
nth
)
sum
=
0
array
=
Array
.
new
(
nth
+
1
)
{
true
}
array
[
0
]
=
false
array
[
1
]
=
false
i
=
2
while
(
i
<=
nth
)
do
if
(
array
[
i
])
then
if
(
i
*
i
<
nth
)
then
array
[
i
*
i
]
=
false
;
end
factor
=
1
while
(
i
+
factor
*
i
<
nth
)
do
array
[
i
+
i
*
factor
]
=
false
factor
+=
1
end
end
i
+=
1
end
i
=
0
;
while
(
i
<=
nth
)
do
if
array
[
i
]
then
sum
+=
i
end
i
+=
1
end
return
sum
end
puts
problem10
(
2_000_000
)
#problem 11
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment