Skip to content

Instantly share code, notes, and snippets.

View mvw's full-sized avatar

Marc van Woerkom mvw

  • Germany, European Union
View GitHub Profile
n = 10000000 # 10M
n.times do |k|
tk = k * (k+1) / 2
if tk.odd?
next
end
tkh = tk / 2
kh = (Math::sqrt(8*tkh + 1) - 1.0) / 2.0
khf = kh.floor
if kh > khf
0 -> 0 -> 0 -> 0
3 -> 6 -> 3 -> 2
20 -> 210 -> 105 -> 14
119 -> 7140 -> 3570 -> 84
696 -> 242556 -> 121278 -> 492
4059 -> 8239770 -> 4119885 -> 2870
23660 -> 279909630 -> 139954815 -> 16730
137903 -> 9508687656 -> 4754343828 -> 97512
803760 -> 323015470680 -> 161507735340 -> 568344
4684659 -> 10973017315470 -> 5486508657735 -> 3312554
#!/usr/bin/env ruby
def check
min = -32
ma = 4 # 2^5 = 32 > 22
mb = 2 # 3^3 = 27 > 22
mc = 2 # 4^3 = 64 > 22
md = 1 # 5^2 = 25 > 22
me = 1 # 6^2 = 36 > 22
puts "checking"
@eps = 1e-6
def int?(x)
return (x - x.floor).abs < @eps
end
def zero?(x)
return x.abs < @eps
end
library(lpSolveAPI)
# S, G, M, B, C, Y
lprec <- make.lp(0,60)
lp.control(lprec,sense="max")
# B10 - C10 + Y10 + S10 + G5 + M2
set.objfn(lprec,
c(
0,0,0,0,0, 0,0,0,0,1,
require 'set'
# return sign of x
def sgn(x)
if x > 0
return 1
elsif x == 0
return 0
end
return -1
--- test
[a1, b1) = [1, 10), dx1 = 2, [a2, b2) = [-5, 20), dx2 = 5
[a1, b1) = [1, 10), dx1 = 2, [a2, b2) = [-5, 20), dx2 = 5
There might be solutions..
2*(12) - 5 * (6) = -6 (goal: -6, true)
some elements of the unbounded intersection:
k = -5: x = -25
k = -4: x = -15
k = -3: x = -5
k = -2: x = 5
require 'matrix'
$a1 = Vector[-4.01, 0, -1.9]
$a2 = Vector[ 4.01, 0, -1.9]
$c1 = Vector[-2.01, 0, -1.9]
$c2 = Vector[ 2.01, 0, -1.9]
def r(t)
(t**3)*($a2 + 3*($c1 - $c2) - $a1) + 3*(t**2)*($a1 - 2*$c1 + $c2) + 3*t*($c1 - $a1) + $a1
$text = "You want to know (in a polynomial time) the minimum cost of making all of the strings the same"
def number_len(k)
return Math::log10(k).floor + 1
end
$words = $text.upcase
.split(/[^A-Z]/)
.select { |w| not w.empty? }
# test for no occurence of '000'
def l0(s)
res = (/\A(1|01|001)*(|0|00)(1|10|100)*\Z/ =~ s) != nil
return res
end
def m0(s)
res = (/000/ =~ s).nil?
return res
end