/
index.html
90 lines (83 loc) · 3.71 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Primes Finder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="/public/og.webp">
<meta property="og:title" content="Primes Finder">
<meta property="og:description" content="Sieve of Eratosthenes using Go and WebAssembly.">
<meta property="og:image" content="https://primes-finder.vercel.app/og.webp">
<meta property="og:url" content="https://primes-finder.vercel.app/">
<meta property="og:type" content="website">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@buarki">
<meta name="twitter:image" content="https://primes-finder.vercel.app/og.webp">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="font-sans bg-gray-100">
<div class="min-h-screen flex flex-col p-3">
<header class="flex justify-between items-center bg-white shadow-lg p-4">
<a href="/" class="font-bold text-blue-500">Primes Finder</a>
<ul class="flex">
<li>
<a href="https://github.com/buarki/primes-finder" target="_blank" title="Project on Github" class="font-bold text-blue-500 hover:text-white hover:bg-blue-500 p-2">Github</a>
</li>
</ul>
</header>
<div class="flex-grow mt-3">
<h1 class="font-bold text-3xl">Primes Finder</h1>
<label for="numberInput" class="mt-2">Inform the interval limit to seek for prime numbers:</label>
<input title="Type numbers >=1 and <= 200000" id="numberInput" type="number" min="1" max="200000" class="bg-gray-300 rounded p-2 shadow-md">
<button title="Search the prime numbers on the given interval" class="bg-blue-500 hover:bg-blue-400 text-white font-bold p-2 mt-2" onclick="findPrimesOnClick()">Find</button>
<p id="result" class="mt-2"></p>
<ul id="primeList" class="list-disc list-inside mt-2"></ul>
</div>
<footer class="flex items-center justify-center bg-white p-4 mt-3">
<p title="Visit author's site.">Made with love by <a target='_blank' class='underline hover:bg-blue-400 p-1 hover:text-white' href='https://buarki.com'>buarki.com.</a></p>
</footer>
</div>
<script src="/public/wasm_exec.js"></script>
<script>
const wasmFile = '/public/main.wasm';
const BASE_10 = 10;
const MAX_ALLOWED_NUMBER = 200_000;
const go = new Go();
function findPrimesOnClick() {
const limit = Number.parseInt(document.getElementById("numberInput").value, BASE_10);
if (limit < 0) {
alert("Only allowed numbers >= 1");
return;
}
if (limit > MAX_ALLOWED_NUMBER) {
alert("Only allowed numbers <= 200000");
return;
}
if (Number.isNaN(limit)) {
alert("Type a valid number");
return;
}
WebAssembly.instantiateStreaming(fetch(wasmFile), go.importObject).then((result) => {
go.run(result.instance);
const rawPrimeResults = findPrimes(limit);
const treatedPrimeNumbers = JSON.parse(JSON.stringify(rawPrimeResults));
if (treatedPrimeNumbers == null) {
alert("Ops, something went wrong :(");
return;
}
document.getElementById("result").innerHTML = `The prime numbers until ${limit} are:`;
renderPrimes(treatedPrimeNumbers);
});
}
function renderPrimes(primes) {
const primeList = document.getElementById("primeList");
primeList.innerHTML = "";
primes.forEach((prime) => {
const listItem = document.createElement("li");
listItem.textContent = prime;
primeList.appendChild(listItem);
});
}
</script>
</body>
</html>