Skip to content

Commit

Permalink
2023-12-07 01:10:08
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/karastuba-algorithm.md
  • Loading branch information
gyunseo committed Dec 6, 2023
1 parent 6f2305d commit 1718408
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
28 changes: 20 additions & 8 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@
"source": false
}
}
},
{
"id": "07aa7999fa9f10a2",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/karastuba-algorithm.md",
"mode": "source",
"source": false
}
}
}
],
"currentTab": 1
"currentTab": 2
}
],
"direction": "vertical"
Expand Down Expand Up @@ -98,7 +110,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/brute-force-job-assignment-problem.md",
"file": "src/content/blog/karastuba-algorithm.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -115,7 +127,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/brute-force-job-assignment-problem.md",
"file": "src/content/blog/karastuba-algorithm.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -138,7 +150,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/brute-force-job-assignment-problem.md"
"file": "src/content/blog/karastuba-algorithm.md"
}
}
}
Expand All @@ -161,10 +173,12 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "29d8a0e02ee4ac43",
"active": "07aa7999fa9f10a2",
"lastOpenFiles": [
"src/content/blog/selection-sort-algorithm.md",
"src/content/blog/karastuba-al.md",
"src/content/blog/karastuba-algorithm.md",
"src/content/blog/brute-force-job-assignment-problem.md",
"src/content/blog/selection-sort-algorithm.md",
"src/content/blog/sequential-search-algorithm.md",
"src/content/blog/brute-force-string-matching-algorithm.md",
"src/content/blog/brute-force-traveling-salesman-problem.md",
Expand Down Expand Up @@ -194,8 +208,6 @@
"src/content/blog/install-asdf-on-ubuntu-linux-and-ohmyzsh.md",
"src/content/blog/computer-network-chapter-7-icmp.md",
"src/content/blog/computer-network-chapter-7-internet-protocol.md",
"src/content/blog/computer-network-chapter-7-packet-switching.md",
"src/content/blog/computer-network-chapter-7-network-layer.md",
"ostep-pdfs/40_FS-implementation.pdf",
"public/favicon.svg",
"public/doggo-og.png",
Expand Down
62 changes: 62 additions & 0 deletions src/content/blog/karastuba-algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,65 @@ $$

`L = 20`, `N = 20`, `M = 20 + 24 - (4 - 8)(5 - 3) = 52`
`xy = 20 * 10^2 + 52 * 10^1 + 24 = 2000 + 520 + 24 = 2544`

## `python` 코드

```python
import sys

sys.setrecursionlimit(10**9)


def get_karatsuba(x, y):
if x < 10 or y < 10:
return x * y

n = max(len(str(x)), len(str(y)))
n2 = n // 2

a = x // 10**n2
b = x % 10**n2
c = y // 10**n2
d = y % 10**n2

ac = get_karatsuba(a, c)
bd = get_karatsuba(b, d)
ad_bc = get_karatsuba(a + b, c + d) - ac - bd

result = ac * 10 ** (2 * n2) + ad_bc * 10**n2 + bd

return result


if __name__ == "__main__":
x = 2462
y = 8014
print(get_karatsuba(x, y))

```

## How to Run

python version: `3.11.6`

### Run `main.py`

```
pip install pipenv
pipenv --python 3.11.6
pipenv run python3 main.py
```

### Input

`2462``8014`를 곱하는 상황

### Output

```zsh
19730468
```

## Execution Image

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1701878947/image_yltmt8.png)

0 comments on commit 1718408

Please sign in to comment.