-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prime
- Loading branch information
Showing
9 changed files
with
178 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from flask import Flask, request, render_template, jsonify | ||
import random | ||
|
||
app = Flask(__name__) | ||
|
||
def is_prime_miller_rabin(n, k=5): | ||
if n <= 1: | ||
return False | ||
if n <= 3: | ||
return True | ||
|
||
def is_witness(a, n): | ||
r, s = 0, n - 1 | ||
while s % 2 == 0: | ||
r += 1 | ||
s //= 2 | ||
|
||
x = pow(a, s, n) | ||
if x == 1 or x == n - 1: | ||
return False | ||
|
||
for _ in range(r - 1): | ||
x = pow(x, 2, n) | ||
if x == n - 1: | ||
return False | ||
|
||
return True | ||
|
||
for _ in range(k): | ||
a = random.randint(2, n - 2) | ||
if is_witness(a, n): | ||
return False | ||
|
||
return True | ||
|
||
@app.route('/') | ||
def index(): | ||
return render_template('index.html') | ||
|
||
@app.route('/check_prime', methods=['POST']) | ||
def check_prime(): | ||
data = request.get_json() | ||
number_str = data['number'] | ||
|
||
try: | ||
number = int(number_str) | ||
if number < 0: | ||
return jsonify({"result": "Negative Numbers cannot be prime"}) | ||
except ValueError: | ||
return jsonify({"result": "Invalid Number format"}) | ||
|
||
if is_prime_miller_rabin(number): | ||
result = "Probably Prime" | ||
else: | ||
result = "Not Prime" | ||
|
||
return jsonify({"result": result}) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
function clearHistory() { | ||
localStorage.removeItem('searchHistory'); | ||
const historyBody = document.getElementById('historyBody'); | ||
const message = document.getElementById('message'); | ||
message.style.animation = 'none'; | ||
void message.offsetWidth; | ||
message.style.animation = null; | ||
message.style.display = 'block'; | ||
|
||
setTimeout(() => { | ||
message.style.display = 'none'; | ||
}, 3000); | ||
|
||
historyBody.innerHTML = ''; | ||
} | ||
|
||
const clearHistoryButton = document.getElementById('clearHistoryButton'); | ||
clearHistoryButton.addEventListener('click', clearHistory); | ||
|
||
function checkPrime() { | ||
const numberInput = document.getElementById('numberInput').value; | ||
const resultElement = document.getElementById('result'); | ||
const historyTable = document.getElementById('historyTable'); | ||
const historyBody = document.getElementById('historyBody'); | ||
|
||
if (numberInput === '') { | ||
resultElement.textContent = 'Please Enter a Valid Number'; | ||
return; | ||
} | ||
|
||
if (BigInt(numberInput) >= 10n ** 1000n) { | ||
resultElement.textContent = 'Maximum Limit Reached'; | ||
return; | ||
} | ||
|
||
|
||
try { | ||
const numBigInt = BigInt(numberInput); | ||
|
||
if (numBigInt.toString().length > 1000) { | ||
resultElement.textContent = 'Maximum Limit Reached (1000 digits)'; | ||
return; | ||
} | ||
|
||
fetch('/check_prime', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ number: numBigInt.toString() }), | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
const isPrimeResult = data.result; | ||
resultElement.textContent = `${numberInput} is ${isPrimeResult}`; | ||
|
||
const searchHistory = JSON.parse(localStorage.getItem('searchHistory')) || []; | ||
searchHistory.push({ number: numberInput, isPrime: isPrimeResult }); | ||
|
||
if (searchHistory.length > 10) { | ||
searchHistory.shift(); | ||
} | ||
|
||
localStorage.setItem('searchHistory', JSON.stringify(searchHistory)); | ||
|
||
historyBody.innerHTML = ''; | ||
searchHistory.forEach((item) => { | ||
const row = document.createElement('tr'); | ||
const numberCell = document.createElement('td'); | ||
const resultCell = document.createElement('td'); | ||
|
||
numberCell.textContent = item.number; | ||
resultCell.textContent = item.isPrime; | ||
|
||
row.appendChild(numberCell); | ||
row.appendChild(resultCell); | ||
historyBody.appendChild(row); | ||
}); | ||
}) | ||
.catch(error => { | ||
console.error('Error:', error); | ||
}); | ||
} catch (error) { | ||
resultElement.textContent = 'Please Enter a Valid Number'; | ||
} | ||
} | ||
|
||
window.addEventListener('load', () => { | ||
const historyBody = document.getElementById('historyBody'); | ||
const searchHistory = JSON.parse(localStorage.getItem('searchHistory')) || []; | ||
|
||
const displayedHistory = searchHistory.slice(-100); | ||
historyBody.innerHTML = ''; | ||
displayedHistory.forEach((item) => { | ||
const row = document.createElement('tr'); | ||
const numberCell = document.createElement('td'); | ||
const resultCell = document.createElement('td'); | ||
|
||
numberCell.textContent = item.number; | ||
resultCell.textContent = item.isPrime; | ||
|
||
row.appendChild(numberCell); | ||
row.appendChild(resultCell); | ||
historyBody.appendChild(row); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters