Thursday, November 10, 2011

Euler Problem 30

My attempt to solve Project Euler no. 30 in Ruby

a = 1..9999

def ret_sum_of_power(number)
  numbers_array = number.to_a
  my_total = 0
  numbers_array.each do |my_num|
    my_total = my_total + my_num ** 4
  end
  return my_total
end

a.each do |my_a|
  if ret_sum_of_power(my_a) == my_a
    puts my_a
  end
end

Euler Problem 29

My try to solve Project Euler no. 29 using Ruby

a = 2..100
b = 2..100

ans = Array.new

a.each do |my_a|
  b.each do |my_b|
    ans << my_a ** my_b
  end
end

puts ans.uniq!.count

Euler Problem 22

My try to solve Project Euler no 22 using Ruby

def get_file_as_string(filename)
  data = ''
  f = File.open(filename, "r") 
  f.each_line do |line|
    data += line
  end
  return data
end

def calc_alpha(alpha)
  ttl = 0
  i = 0
  begin
    ttl += alpha[i].ord - 64
    i += 1
  end until i == alpha.length
  return ttl
end

##### MAIN #####

names_data = get_file_as_string '~\src\euler22\names.txt'

sompret = names_data.gsub('"','')
array = sompret.split(/,/)

array.sort!

running_total = 0

array.each_index do |idx|
  running_total += calc_alpha(array[idx]) * (idx + 1)
end

print 'Result:: '
print running_total

Euler Problem 21

My try to solve Project Euler Problem 21 using Ruby

def d(num)
  num_array = Array.new
  
  # this num/2 is used to cut the time for this execution into half
  for i in 1..num/2   
    if num % i == 0 #&& i != num
      num_array << i
    end
  end
  total_num = 0
  total_num = num_array.inject(:+).to_i
  return total_num
end

def cmp(num,num_c)
  if d(num) == num_c  && num != num_c
    return true
  else
    return false
  end
end

total_sum = 0

for i in 1..10000
  if cmp(d(i), i) == true
    puts i
    total_sum += i
  end
end
puts total_sum

Euler Problem 19

My try to solve Project Euler problem no. 19 in Ruby

require 'date'

time = DateTime.new(1901,1,1)

concur = 0

while time.year < 2001 do
  if time.wday == 0
    puts time.strftime("%Y-%m-%d") + ": Yes"
    concur += 1
  end
  
  time = time >> 1
end

puts "Total concurrent: " + concur.to_s

Euler Problem 17

My try to solve Project Euler problem no. 17

require 'rubygems'
require 'linguistics'    # yeah, I use linguistics gem. Easier rather than doing the function by myself :P

Linguistics::use( :en )

totallength = 0

1001.times do |x|
  if x > 0
    #puts x.en.numwords.delete(" -").length
    totallength += x.en.numwords.delete(" -").length
  end
end

puts totallength

Wednesday, November 9, 2011

Euler Problem 145

My try to solve project euler problem no. 145 using Ruby

#max = 10**9
max = 89990001 #no other result beyond this value, try for yourself :)

total_reversible = 0

(1..max).each do |my_num_a|
  if my_num_a % 2 != 0
    my_result = (my_num_a + my_num_a.to_s.reverse.to_i).to_s
    my_scan = my_result.gsub(/[02468]/,'*')
    if my_scan === my_result
      #puts my_num_a
      total_reversible += 1
    end
  end
end
puts total_reversible * 2 # why multiply by two? because each value and its reversible value are accounted

Sunday, November 6, 2011

Installing tag for abap in gVim in windows

Hurrah! At last finished installing taglist in Vim for ABAP language, but still very minimal tag recognition (for windows).