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 Cuban Prime Calculator #603

Merged
merged 9 commits into from
Feb 24, 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
24 changes: 24 additions & 0 deletions Calculators/Cuban-Prime-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# <p align="center">Cuban Prime Checker</p>

## Description :-

Cuban Prime Checker is a web application that allows users to identify and find Cuban prime numbers within a specified range.

* Checks if a number is a Cuban prime.
* Finds Cuban prime numbers within a specified range.

## What is a Cuban Prime?

A Cuban prime is a prime number that is a solution to the Diophantine equation x^3 + y^3 = z^3 + 1, where x, y, and z are positive integers. In other words, it's a special type of prime number related to the sum of cubes.

For example, 2, 19, 37 are Cuban primes as they satisfy the mentioned Diophantine equation.

## Tech Stacks :-

- HTML
- CSS
- JavaScript


## Screenshots :-
![Cuban](https://github.com/Rakesh9100/CalcDiverse/assets/125949765/61d9e288-e8bd-4be2-8a09-babb2bac3f1b)
57 changes: 57 additions & 0 deletions Calculators/Cuban-Prime-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!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>Cuban Prime Calculator</title>
</head>

<body>

<h1 class="title">Cuban Prime Calculator</h1>
<div class="information">
<h2>What is a Cuban Prime?</h2>
<p>
A Cuban prime is a prime number that is a solution to the Diophantine equation <em>x^3 + y^3 = z^3 + 1</em>, where x, y, and z are positive integers.
It's a special type of prime number related to the sum of cubes.
</p>

<h2>Formula:</h2>
<p>
The Diophantine equation for Cuban primes is <em>x^3 + y^3 = z^3 + 1</em>.
</p>

<h2>Solution Process:</h2>
<p>
When checking a number for Cuban primality, the application verifies if the given number satisfies the Diophantine equation.
The application then checks if both the calculated value and the given number are prime. If the conditions are met, the number is identified as a Cuban Prime.
</p>
</div>
<div class="calculator">
<div class="section">
<p class="calculator-title">Check Single Cuban Prime</p>
<label for="number">Enter a number:</label>
<input type="number" id="number" placeholder="Enter a number">
<button onclick="checkCubanPrime()">Check</button>
<p id="resultSingle"></p>
</div>

<div class="section">
<p class="calculator-title">Check Cuban 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 onclick="checkCubanPrimesInRange()">Check</button>
<p id="rangeResult"></p>
</div>
</div>



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

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

function cubeOfNum(num) {
return num * num * num;
}

function isCubanPrime(num) {
let res = cubeOfNum(num) - cubeOfNum(num - 1);
return isPrime(res) && isPrime(num);
}

function getCubanSolution(num) {
let res = cubeOfNum(num) - cubeOfNum(num - 1);
return `<br>The cube of ${num} is ${cubeOfNum(num)}.<br>The cube of ${num - 1} is ${cubeOfNum(num - 1)}.<br>The difference is ${res}.<br>This number is Cuban Prime because both ${num} and ${res} are prime.`;
}

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

if (!validateInput(number)) {
alert('Please enter a valid positive integer.');
return;
}

let result = isCubanPrime(parseInt(number));
let message = result ? `${number} is a Cuban Prime number. ${getCubanSolution(parseInt(number))}` : `${number} is not a Cuban Prime number.`;

document.getElementById('resultSingle').innerHTML = `<span class="${result ? 'cuban-prime' : 'not-cuban-prime'}">${message}</span>`;
}

function checkCubanPrimesInRange() {
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 cubanPrimes = [];

for (let i = fromRange; i <= toRange; i++) {
if (isCubanPrime(i)) {
cubanPrimes.push(i);
}
}

if (cubanPrimes.length > 0) {
document.getElementById('rangeResult').innerHTML = `<span class="cuban-prime-result">Cuban Prime numbers in the range ${fromRange} to ${toRange}: ${cubanPrimes.join(', ')}</span>`;
} else {
document.getElementById('rangeResult').innerHTML = `There are no Cuban Prime numbers in the range ${fromRange} to ${toRange}.`;
}
}

function validateInput(number) {
return /^(0|[1-9]\d*)$/.test(number);
}
120 changes: 120 additions & 0 deletions Calculators/Cuban-Prime-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
body {
font-family: 'Roboto', sans-serif;
text-align: center;
margin: 0;
padding: 0;
background: linear-gradient(135deg, #2980B9, #6DD5FA);
color: #fff;
}

.calculator {
display: flex;
justify-content: space-around;
align-items: flex-start;
margin: 50px auto;
max-width: 900px;
}

.section {
width: 45%;
padding: 20px;
box-sizing: border-box;
background: linear-gradient(45deg, #1E2A39, #394A5E);
border: 1px solid #00f;
border-radius: 15px;
box-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
margin-bottom: 20px;
}

input {
padding: 10px;
font-size: 16px;
margin-top: 10px;
width: 100%;
box-sizing: border-box;
background-color: #fff;
border: none;
border-radius: 5px;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #3498DB;
color: #fff;
border: none;
border-radius: 5px;
margin-top: 10px;
transition: background-color 0.3s, box-shadow 0.3s;
}

button:hover {
background-color: #2980B9;
box-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
}

p {
font-size: 18px;
margin-top: 20px;
color: #fff;
}
.cuban-prime-result {
color: #2ECC71;
}

.title {
padding: 20px;
margin: 0;
font-size: 36px;
font-weight: 900;
color: #FFC300;
transition: color 0.3s, transform 0.3s;
}

.title:hover {
color: #E74C3C;
transform: scale(1.1);
}

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

.information {
text-align: justify;
margin-top: 30px;
color: #fff;
max-width: 800px;
margin-left: auto;
margin-right: auto;
border: 1px solid #00f;
border-radius: 15px;
padding: 20px;
box-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
}


.information h2 {
color: #FFC300;
font-size: 24px;
margin-bottom: 10px;
}

.information p {
font-size: 16px;
line-height: 1.6;
margin-bottom: 20px;
}
.cuban-prime {
color: #2ECC71; }

.not-cuban-prime {
color: #E74C3C;
}
.rangeResult{
color: #2ECC71;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,20 @@ <h3>Calculates the time in which you can payoff your Credits.</h3>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Cuban Prime Calculator</h2>
<h3>Checks if a number is cuban prime or not and finds cuban prime numbers in a range.</h3>
<div class="card-footer">
<a href="./Calculators/Cuban-Prime-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Cuban-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