Skip to content

Instantly share code, notes, and snippets.

@nck2
Created September 12, 2018 05:48
Show Gist options
  • Save nck2/75f69265a5ad0c40b0e33f30a1d9e538 to your computer and use it in GitHub Desktop.
Save nck2/75f69265a5ad0c40b0e33f30a1d9e538 to your computer and use it in GitHub Desktop.
정사면체
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [서울과학고 기통 기출문제]\n",
"Q. 각 면에 $1,2,3,6$의 숫자가 하나씩 적혀 있는 정사면체가 있다. 이 정사면체를 세 번 던져서 얻은 밑면에 적힌 숫자를\n",
"나온 순서대로 $x,y,z$라 하자. \n",
"다음 물음에 답하시오.\n",
"- (1) $xyz$가 6의 배수가 될 확률을 구하시오\n",
"- (2) $xy+yz+zx$가 3의 배수가 될 확률을 구하시오"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 시뮬레이션 해보기"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tetr = [1,2,3,6]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choice(tetr)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"random.choices?"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 2, 3]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choices(tetr,k=3)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"def myt(mytetr,mycount):\n",
" \"\"\"xyz가 6의배수일 확률\"\"\"\n",
" su =0\n",
" for i in range(mycount):\n",
" if random.choice(tetr)*random.choice(tetr)*random.choice(tetr) % 6 ==0:\n",
" # print(mytetr[:3])\n",
" su =su+1\n",
" return su/mycount\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7671"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myt(tetr,10000)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def myt2(mytetr,mycount):\n",
" \"\"\"xy+yz+zx가 3의배수일 확률\"\"\"\n",
" su =0\n",
" \n",
" for i in range(mycount):\n",
" x,y,z = random.choices(mytetr,k=3)\n",
" if (x*y+y*z+z*x) % 3 ==0:\n",
" # print(mytetr[:3])\n",
" su =su+1\n",
" return su/mycount"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x,y,z =[1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5279"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myt2(tetr,10000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 실제 계산해보기"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import itertools\n",
"import fractions"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"itertools.product?"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fractions.Fraction?"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 (1, 1, 1)\n",
"2 (1, 1, 2)\n",
"3 (1, 1, 3)\n",
"4 (1, 1, 6)\n",
"5 (1, 2, 1)\n",
"6 (1, 2, 2)\n",
"7 (1, 2, 3)\n",
"8 (1, 2, 6)\n",
"9 (1, 3, 1)\n",
"10 (1, 3, 2)\n",
"11 (1, 3, 3)\n",
"12 (1, 3, 6)\n",
"13 (1, 6, 1)\n",
"14 (1, 6, 2)\n",
"15 (1, 6, 3)\n",
"16 (1, 6, 6)\n",
"17 (2, 1, 1)\n",
"18 (2, 1, 2)\n",
"19 (2, 1, 3)\n",
"20 (2, 1, 6)\n",
"21 (2, 2, 1)\n",
"22 (2, 2, 2)\n",
"23 (2, 2, 3)\n",
"24 (2, 2, 6)\n",
"25 (2, 3, 1)\n",
"26 (2, 3, 2)\n",
"27 (2, 3, 3)\n",
"28 (2, 3, 6)\n",
"29 (2, 6, 1)\n",
"30 (2, 6, 2)\n",
"31 (2, 6, 3)\n",
"32 (2, 6, 6)\n",
"33 (3, 1, 1)\n",
"34 (3, 1, 2)\n",
"35 (3, 1, 3)\n",
"36 (3, 1, 6)\n",
"37 (3, 2, 1)\n",
"38 (3, 2, 2)\n",
"39 (3, 2, 3)\n",
"40 (3, 2, 6)\n",
"41 (3, 3, 1)\n",
"42 (3, 3, 2)\n",
"43 (3, 3, 3)\n",
"44 (3, 3, 6)\n",
"45 (3, 6, 1)\n",
"46 (3, 6, 2)\n",
"47 (3, 6, 3)\n",
"48 (3, 6, 6)\n",
"49 (6, 1, 1)\n",
"50 (6, 1, 2)\n",
"51 (6, 1, 3)\n",
"52 (6, 1, 6)\n",
"53 (6, 2, 1)\n",
"54 (6, 2, 2)\n",
"55 (6, 2, 3)\n",
"56 (6, 2, 6)\n",
"57 (6, 3, 1)\n",
"58 (6, 3, 2)\n",
"59 (6, 3, 3)\n",
"60 (6, 3, 6)\n",
"61 (6, 6, 1)\n",
"62 (6, 6, 2)\n",
"63 (6, 6, 3)\n",
"64 (6, 6, 6)\n"
]
}
],
"source": [
"for j,i in enumerate(itertools.product([1,2,3,6],repeat=3),1):\n",
" print(j,i)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"첫번째 답은 49/64, 근사 0.765625\n",
"두번째 답은 17/32, 근사 0.53125\n"
]
}
],
"source": [
"msu,msu2 =0,0\n",
"for i in itertools.product([1,2,3,6],repeat=3):\n",
" x,y,z = i\n",
" if (x*y*z)% 6 == 0:\n",
" msu = msu+1\n",
" if (x*y+y*z+z*x)% 3 ==0:\n",
" msu2 = msu2+1\n",
"print(\"첫번째 답은 {}, 근사 {}\".format(fractions.Fraction(msu/64), msu/64))\n",
"print(\"두번째 답은 {}, 근사 {}\".format(fractions.Fraction(msu2/64), msu2/64))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment