-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Circular Prime Calculator (#597)
- Loading branch information
1 parent
d8acd94
commit 5240aab
Showing
5 changed files
with
261 additions
and
0 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
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) | ||
|
||
|
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,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> |
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,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 = ''; | ||
} |
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,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; | ||
} |
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