Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Circular Prime Calculator #597

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Calculators/Circular-Prime-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# <p align="center">Circular Prime Checker</p>

## Description :-

Circular Prime Checker is a web application that allows users to check if a given number is a Circular Prime and find bouncy numbers within a specified range.

* Checks if a number is a Circular Prime Checker.
* Finds Circular Prime numbers within a specified range.

## What is a Circular Prime ?

A prime number is said to be a circular prime if after any cyclic permutations of the digits, it remains a prime.

Examples:

Input : n = 113
Output : Yes
All cyclic permutations of 113 (311
and 131) are prime.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Screenshots :-

![CPC](https://github.com/Rakesh9100/CalcDiverse/assets/125949765/34d359b2-23d5-456c-b88f-51fb413bebbc)


39 changes: 39 additions & 0 deletions Calculators/Circular-Prime-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Circular Prime Checker</title>
</head>
<body>

<p class="title">Circular Prime Checker</p>



<div class="calculator">
<div class="section">
<p class="calculator-title">Circular Prime Checker</p>
<label for="number">Enter a number:</label>
<input type="number" id="numberInput" placeholder="Enter a number">
<button class="check" onclick="checkCircularPrime()">Check</button>
<button class="clear" onclick="clearSection()">Clear</button>
<p id="result"></p>
</div>

<div class="section">
<p class="calculator-title">Find Circular Primes in Range</p>
<label for="fromRange">From:</label>
<input type="number" id="fromRange" placeholder="Enter from number">
<label for="toRange">To:</label>
<input type="number" id="toRange" placeholder="Enter to number">
<button class="check" onclick="findCircularPrimesInRange()">Find</button>
<button class="clear" onclick="clearRangeSection()">Clear</button>
<p id="rangeResult"></p>
</div>
</div>

<script src="script.js"></script>
</body>
</html>
99 changes: 99 additions & 0 deletions Calculators/Circular-Prime-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

function checkCircularPrime() {
let number = document.getElementById('numberInput').value;

if (!/^\d+$/.test(number)) {
alert('Please enter a valid positive integer.');
return;
}

let numStr = number.toString();
let isCircularPrime = true;
let primePermutations = [];

for (let i = 0; i < numStr.length; i++) {
let rotatedNumber = parseInt(numStr.slice(i) + numStr.slice(0, i));
primePermutations.push(rotatedNumber);

if (!isPrime(rotatedNumber)) {
isCircularPrime = false;
break;
}
}

let result;
if (isCircularPrime) {
result = `Yes, it is a Circular Prime. Circular Permutations: ${primePermutations.join(', ')}`;
} else {
result = `No, it is not a Circular Prime. Circular Permutations: ${primePermutations
.map((num, index) => `${num} (${isPrime(num) ? 'Prime' : 'Not Prime'})`)
.join(', ')}`;
}

document.getElementById('result').innerText = result;
}

function findCircularPrimesInRange() {
let fromRange = parseInt(document.getElementById('fromRange').value);
let toRange = parseInt(document.getElementById('toRange').value);

if (isNaN(fromRange) || isNaN(toRange) || fromRange >= toRange) {
alert('Please enter valid range values.');
return;
}

let circularPrimes = [];

for (let num = fromRange; num <= toRange; num++) {
if (isPrime(num)) {
let numStr = num.toString();
let isCircularPrime = true;
let primePermutations = [];

for (let i = 0; i < numStr.length; i++) {
let rotatedNumber = parseInt(numStr.slice(i) + numStr.slice(0, i));
primePermutations.push(rotatedNumber);

if (!isPrime(rotatedNumber)) {
isCircularPrime = false;
break;
}
}

if (isCircularPrime) {
circularPrimes.push({ number: num, permutations: primePermutations });
}
}
}

let result;
if (circularPrimes.length > 0) {
result = `Circular Primes in the range ${fromRange} to ${toRange}: ${circularPrimes
.map(cp => `${cp.number} `)
.join(', ')}`;
} else {
result = `There are no Circular Primes in the range ${fromRange} to ${toRange}.`;
}

document.getElementById('rangeResult').innerText = result;
}

function clearSection() {
document.getElementById('numberInput').value = '';
document.getElementById('result').innerText = '';
}

function clearRangeSection() {
document.getElementById('fromRange').value = '';
document.getElementById('toRange').value = '';
document.getElementById('rangeResult').innerText = '';
}
78 changes: 78 additions & 0 deletions Calculators/Circular-Prime-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
body {
font-family: 'Arial', sans-serif;
text-align: center;
margin: 0;
padding: 0;
background-color: #091020;
color: #fff;
}

.title {
padding: 20px;
margin: 0;
font-size: 36px;
font-weight: 900;
color: yellow;
padding-top: 90px;
}
.title:hover{
color: red;
scale: 1.3;
}

.calculator {
display: flex;
flex-wrap: wrap;
justify-content: center;
}

.section {
width: 90%;
max-width: 400px;
margin: 20px;
padding: 20px;
box-sizing: border-box;

border: 2px solid #da0808;
border-radius: 15px;
}

.calculator-title {
color: #bbed06;
font-size: 24px;
font-weight: bold;
margin-bottom: 15px;
}

input {
padding: 10px;
font-size: 16px;
margin-top: 10px;
width: 100%;
box-sizing: border-box;
}

label{
font-size: large;
color: white;
}
button {
padding: 10px 20px;
font-size: 20px;
cursor: pointer;
background-color: yellow;
color: red;
border: none;
border-radius: 5px;
margin-top: 10px;
}

button:hover {
background-color: rgb(235, 192, 21);
}

p {
font-size: 18px;
margin-top: 20px;
color: #d4eb0b;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,20 @@ <h3>Generates the QR Code according to the given input.</h3>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Circular Prime Calculator</h2>
<h3>Checks if a number is circular prime and finds circular prime numbers in a range.</h3>
<div class="card-footer">
<a href="./Calculators/Circular-Prime-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Circular-Prime-Calculator" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code"></img>
</a>
</div>
</div>
</div>
</div>

<!-- Calculator Section Ends Here -->
Expand Down