Skip to content

Commit

Permalink
created
Browse files Browse the repository at this point in the history
  • Loading branch information
genkuroki committed Apr 14, 2024
1 parent b17eca8 commit ef71a00
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 0 deletions.
223 changes: 223 additions & 0 deletions 0048/suzzukes problem 2021-04-14.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "38928e50-521a-4ce5-a521-5fbbba3c96aa",
"metadata": {},
"source": [
"https://x.com/dannchu/status/1779438301398892932"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3c870937-6d4f-4bfb-a5be-7d0a644b34fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 19.002045 seconds (382.00 M allocations: 25.895 GiB, 12.45% gc time)\n",
" 19.192160 seconds (382.00 M allocations: 25.894 GiB, 12.48% gc time)\n",
" 20.750222 seconds (382.00 M allocations: 25.895 GiB, 14.02% gc time)\n"
]
},
{
"data": {
"text/plain": [
"0.20991765"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Distributions\n",
"\n",
"function prob_orig(n)\n",
" ab = rand(Bernoulli(0.6), n)\n",
" ac = rand(Bernoulli(0.75), n)\n",
" bc = rand(Bernoulli(0.6), n)\n",
" k = 0\n",
" for i in 1:n\n",
" if [ab[i], ac[i], bc[i]] == [1, 0, 1] || [ab[i], ac[i], bc[i]] == [0, 1, 0]\n",
" k += 1\n",
" end\n",
" end\n",
" k/n\n",
"end\n",
"\n",
"@time prob_orig(10^8)\n",
"@time prob_orig(10^8)\n",
"@time prob_orig(10^8)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c34046a5-f062-4631-be8b-ca35c7d3ca32",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.861813 seconds\n",
" 0.844786 seconds\n",
" 0.847345 seconds\n"
]
},
{
"data": {
"text/plain": [
"0.21002194"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Distributions\n",
"\n",
"function prob_rev1(n)\n",
" k = 0\n",
" for i in 1:n\n",
" ab = rand(Bernoulli(0.6))\n",
" ac = rand(Bernoulli(0.75))\n",
" bc = rand(Bernoulli(0.6))\n",
" if (ab, ac, bc) == (1, 0, 1) || (ab, ac, bc) == (0, 1, 0)\n",
" k += 1\n",
" end\n",
" end\n",
" k/n\n",
"end\n",
"\n",
"@time prob_rev1(10^8)\n",
"@time prob_rev1(10^8)\n",
"@time prob_rev1(10^8)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "99b46204-fa9d-4331-abb0-a60944592cb2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.770400 seconds\n",
" 0.763928 seconds\n",
" 0.774524 seconds\n"
]
},
{
"data": {
"text/plain": [
"0.21000702"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Distributions\n",
"\n",
"function prob_rev2(n)\n",
" k = 0\n",
" for i in 1:n\n",
" ab = rand(Bernoulli(0.6))\n",
" ac = rand(Bernoulli(0.75))\n",
" bc = rand(Bernoulli(0.6))\n",
" k += ab == bc !== ac\n",
" end\n",
" k/n\n",
"end\n",
"\n",
"@time prob_rev2(10^8)\n",
"@time prob_rev2(10^8)\n",
"@time prob_rev2(10^8)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7f9d5b9f-9383-4b9c-aa7e-39de9ecf44f2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.133616 seconds (19.24 k allocations: 1.236 MiB, 259.61% compilation time)\n",
" 0.114210 seconds (185 allocations: 18.344 KiB)\n",
" 0.108316 seconds (185 allocations: 18.266 KiB)\n"
]
},
{
"data": {
"text/plain": [
"0.21003921"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Distributions\n",
"\n",
"function prob_rev3(n)\n",
" k = zeros(Int, Threads.nthreads())\n",
" Threads.@threads for i in 1:n\n",
" tid = Threads.threadid()\n",
" ab = rand(Bernoulli(0.6))\n",
" ac = rand(Bernoulli(0.75))\n",
" bc = rand(Bernoulli(0.6))\n",
" k[tid] += ab == bc !== ac\n",
" end\n",
" sum(k)/n\n",
"end\n",
"\n",
"@time prob_rev3(10^8)\n",
"@time prob_rev3(10^8)\n",
"@time prob_rev3(10^8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45a88fc2-55c8-42a2-a5f7-e3535477b0d1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,jl:hydrogen"
},
"kernelspec": {
"display_name": "Julia 1.10.2",
"language": "julia",
"name": "julia-1.10"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
96 changes: 96 additions & 0 deletions 0048/suzzukes problem 2021-04-14.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# ---
# jupyter:
# jupytext:
# formats: ipynb,jl:hydrogen
# text_representation:
# extension: .jl
# format_name: hydrogen
# format_version: '1.3'
# jupytext_version: 1.10.3
# kernelspec:
# display_name: Julia 1.10.2
# language: julia
# name: julia-1.10
# ---

# %% [markdown]
# https://x.com/dannchu/status/1779438301398892932

# %%
using Distributions

function prob_orig(n)
ab = rand(Bernoulli(0.6), n)
ac = rand(Bernoulli(0.75), n)
bc = rand(Bernoulli(0.6), n)
k = 0
for i in 1:n
if [ab[i], ac[i], bc[i]] == [1, 0, 1] || [ab[i], ac[i], bc[i]] == [0, 1, 0]
k += 1
end
end
k/n
end

@time prob_orig(10^8)
@time prob_orig(10^8)
@time prob_orig(10^8)

# %%
using Distributions

function prob_rev1(n)
k = 0
for i in 1:n
ab = rand(Bernoulli(0.6))
ac = rand(Bernoulli(0.75))
bc = rand(Bernoulli(0.6))
if (ab, ac, bc) == (1, 0, 1) || (ab, ac, bc) == (0, 1, 0)
k += 1
end
end
k/n
end

@time prob_rev1(10^8)
@time prob_rev1(10^8)
@time prob_rev1(10^8)

# %%
using Distributions

function prob_rev2(n)
k = 0
for i in 1:n
ab = rand(Bernoulli(0.6))
ac = rand(Bernoulli(0.75))
bc = rand(Bernoulli(0.6))
k += ab == bc !== ac
end
k/n
end

@time prob_rev2(10^8)
@time prob_rev2(10^8)
@time prob_rev2(10^8)

# %%
using Distributions

function prob_rev3(n)
k = zeros(Int, Threads.nthreads())
Threads.@threads for i in 1:n
tid = Threads.threadid()
ab = rand(Bernoulli(0.6))
ac = rand(Bernoulli(0.75))
bc = rand(Bernoulli(0.6))
k[tid] += ab == bc !== ac
end
sum(k)/n
end

@time prob_rev3(10^8)
@time prob_rev3(10^8)
@time prob_rev3(10^8)

# %%

0 comments on commit ef71a00

Please sign in to comment.