Skip to content

Commit

Permalink
Prime
Browse files Browse the repository at this point in the history
Prime
  • Loading branch information
Jayesh-JainX committed Oct 25, 2023
2 parents c823e7e + 95bc258 commit ce5fe34
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 110 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Looking for a quick and easy way to check whether a number is prime or not? You'

### 🚀 How to Use

1. Visit our [Prime Number Checker](https://jayesh-jainx.github.io/Prime_Numbers/) page.
1. Visit our [Prime Number Checker](https://jayeshjainx.pythonanywhere.com/) page.

2. Enter the number you want to check for primality into the input field.

Expand Down
60 changes: 60 additions & 0 deletions app.py
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)
Binary file modified images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 0 additions & 103 deletions script.js

This file was deleted.

Binary file added static/back_ground.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions static/script.js
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.
17 changes: 11 additions & 6 deletions index.html → templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/png" sizes="32x32" href="images/logo.png">
<link rel="stylesheet" href="/static/style.css">
<link rel="icon" type="image/png" href="{{ url_for('static', filename='favicon.png') }}">

</head>

<body>
<div class="container">
<h1>Prime Number Checker</h1>
<h1>Prime Number Checkers</h1>
<p>Enter a number to check if it's prime:</p>
<input type="number" id="numberInput" placeholder="Enter a number">

<input type="text" id="numberInput" name="number" placeholder="Enter a Number">

<button onclick="checkPrime()">Check</button>
<p id="result"></p>
</div>
Expand All @@ -40,10 +43,12 @@ <h2>Search History</h2>
</div>

<footer>
<p>Created by Jayesh + Yash</p>
<p>Created by Jayesh Jain</p>
</footer>

<script src="script.js"></script>
<script src="/static/script.js"></script>


</body>

</html>

0 comments on commit ce5fe34

Please sign in to comment.