Skip to content

Commit

Permalink
added armstrong checker
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhhsinghh committed Dec 19, 2024
1 parent 3943d00 commit aa2d1ae
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 116 deletions.
6 changes: 4 additions & 2 deletions Calculators/Armstrong-Number-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description :-

Calculates all the Armstrong Numbers within a range measured by the number of digits.
This tool helps you explore Armstrong numbers. Use the Checker to verify if a specific number is an Armstrong number. Use the Finder to generate Armstrong numbers for a given number of digits.

## Tech Stacks :-

Expand All @@ -12,4 +12,6 @@ Calculates all the Armstrong Numbers within a range measured by the number of di

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/1c41cfd8-dce2-4529-9dcf-a4e33c24e5c8)
![image1](<Screenshot 2024-12-19 223205.png>)
![image2](<Screenshot 2024-12-19 223247.png>)
![image3](<Screenshot 2024-12-19 223326.png>) ![alt text](<Screenshot 2024-12-19 223247-1.png>)
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 44 additions & 14 deletions Calculators/Armstrong-Number-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,55 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Armstrong Number Calculator</title>
<title>Armstrong Number Utility</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<header>
<h1>Armstrong Number Calculator for N-Digits</h1>
</header>
<main>
<div class="container">
<form name="myform">
<label for="no">Enter Number of Digits (should be <=7):</label>
<input type="number" name="numOfDigits" id="no">
<button type="button" onclick="findArmstrongNumbers()">Find Armstrong Numbers</button>
</form>
<div id="armstrongNumbers"></div>
<div id="additionalContent"></div>
<!-- Navigation Bar -->
<nav class="navbar">
<div class="nav-container">
<a href="#" class="logo">Armstrong Utility</a>
<ul class="nav-links">
<li><a href="#" onclick="showSection('description')">Home</a></li>
<li><a href="#" onclick="showSection('checker')">Checker</a></li>
<li><a href="#" onclick="showSection('finder')">Finder</a></li>
</ul>
</div>
</main>
</nav>

<!-- Main Content -->
<div class="container">
<!-- Home Description Section -->
<div id="description" class="card">
<h2>Welcome to Armstrong Utility</h2>
<p>
This tool helps you explore Armstrong numbers, also known as Narcissistic numbers.
<br><br>
- Use the <strong>Checker</strong> to verify if a specific number is an Armstrong number.
- Use the <strong>Finder</strong> to generate Armstrong numbers for a given number of digits.
</p>
</div>

<!-- Checker Section -->
<div id="checker" class="card hidden">
<h2>Armstrong Number Checker</h2>
<p>Check if a specific number is an Armstrong number:</p>
<input type="text" id="number" placeholder="Enter a number" />
<button onclick="checkArmstrong()">Check</button>
<div id="result" class="result"></div>
</div>

<!-- Finder Section -->
<div id="finder" class="card hidden">
<h2>Find Armstrong Numbers</h2>
<p>Find Armstrong numbers by specifying the number of digits:</p>
<input type="number" id="numOfDigits" min="1" max="7" placeholder="Enter number of digits" />
<button onclick="findArmstrongNumbers()">Find Numbers</button>
<div id="armstrongNumbers" class="result"></div>
</div>
</div>

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

Expand Down
87 changes: 48 additions & 39 deletions Calculators/Armstrong-Number-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
// Function to find Armstrong numbers based on user input
function findArmstrongNumbers() {
const numOfDigits = document.forms.myform.numOfDigits.value;
// Show the selected section and hide others
function showSection(sectionId) {
const sections = document.querySelectorAll('.card');
sections.forEach((section) => {
if (section.id === sectionId) {
section.classList.remove('hidden');
} else {
section.classList.add('hidden');
}
});
}

// Validate user input
if (numOfDigits === "" || Number.isNaN(numOfDigits) || numOfDigits < 1 || numOfDigits > 7) {
alert("Please enter a valid number between 1 and 7");
} else {
const n = parseInt(numOfDigits);
const myElement = document.getElementById("armstrongNumbers");
const chunkSize = 1000;
// Check if a number is Armstrong
function checkArmstrong() {
const numberInput = document.getElementById("number").value;
const resultDiv = document.getElementById("result");

let startRange = Math.pow(10, n - 1);
let endRange = Math.pow(10, n);
if (!numberInput || isNaN(numberInput)) {
resultDiv.innerHTML = "<span style='color: red;'>Please enter a valid number.</span>";
return;
}

let armstrongNumbers = [];
const number = parseInt(numberInput, 10);
const digits = numberInput.split("").map(Number);
const power = digits.length;
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, power), 0);

// Process the range in smaller chunks to improve responsiveness
for (let chunkStart = startRange; chunkStart <= endRange; chunkStart += chunkSize) {
let chunkEnd = Math.min(chunkStart + chunkSize, endRange);
if (sum === number) {
resultDiv.innerHTML = `<span style="color: green;">Yes! ${number} is an Armstrong number.</span>`;
} else {
resultDiv.innerHTML = `<span style="color: red;">No, ${number} is not an Armstrong number.</span>`;
}
}

// Check for Armstrong numbers in the current chunk
for (let num = chunkStart; num < chunkEnd; num++) {
if (isArmstrongOptimized(num, n)) {
armstrongNumbers.push(num);
}
}
}
// Find Armstrong numbers
function findArmstrongNumbers() {
const numOfDigits = document.getElementById("numOfDigits").value;
const resultDiv = document.getElementById("armstrongNumbers");

myElement.textContent = "Armstrong Numbers: " + armstrongNumbers.join(", ");
if (!numOfDigits || isNaN(numOfDigits) || numOfDigits < 1 || numOfDigits > 7) {
resultDiv.innerHTML = "<span style='color: red;'>Please enter a valid number between 1 and 7.</span>";
return;
}
}

// Function to check if a number is an Armstrong number
function isArmstrongOptimized(num, power) {
let temp = num;
let sumOfPowers = 0;
const n = parseInt(numOfDigits, 10);
const start = Math.pow(10, n - 1);
const end = Math.pow(10, n);
const armstrongNumbers = [];

// Compute the sum of digits raised to the power
while (temp > 0) {
let digit = temp % 10;
sumOfPowers += Math.pow(digit, power);
temp = Math.floor(temp / 10);
for (let i = start; i < end; i++) {
const digits = i.toString().split("").map(Number);
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, n), 0);
if (sum === i) {
armstrongNumbers.push(i);
}
}
const additionalContent = document.getElementById('additionalContent');
additionalContent.innerText = 'Explanation: A number that is equal to the sum of cubes of its digits.';

// Check whether sum of powers equals the original number or not
return sumOfPowers === num;

resultDiv.innerHTML = `<span>Armstrong Numbers: ${armstrongNumbers.join(", ")}</span>`;
}
144 changes: 83 additions & 61 deletions Calculators/Armstrong-Number-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -1,82 +1,104 @@
/* General Styling */
body {
background-color: rgb(6, 0, 67);
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
background: linear-gradient(135deg, #5f72bd, #9b23ea);
color: #fff;
}

h1 {
color: #c7c3c3;
margin-bottom: 40px;
.hidden {
display: none;
}

.container {
display: flex;
flex-direction: column;
color: rgb(5, 12, 43);
font-size: large;
align-items: center;
justify-content: center;
background-color: white;
border-radius: 30px;
padding: 100px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
/* Navbar */
.navbar {
background: #4e45d5;
padding: 10px 20px;
position: sticky;
top: 0;
z-index: 1000;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

form {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
.nav-container {
display: flex;
justify-content: space-between;
align-items: center;
max-width: 1200px;
margin: 0 auto;
}

label {
margin-bottom: 20px;
.logo {
color: #fff;
font-size: 24px;
font-weight: bold;
text-decoration: none;
}

input {
padding: 8px;
margin-bottom: 20px;
width: 150px;
.nav-links {
list-style: none;
display: flex;
gap: 20px;
margin: 0;
padding: 0;
}

button {
padding: 10px;
background-color: #4e45d5;
color: white;
width: fit-content;
box-shadow: 5px 5px 7px 0px #0000003f;
font-size: 18px;
cursor: pointer;
transition: all 0.5s;
font-weight: 500;
border: solid 3px transparent;
position: relative;
z-index: 1;
.nav-links li {
display: inline;
}

.nav-links a {
color: #fff;
text-decoration: none;
font-size: 16px;
padding: 5px 10px;
transition: background 0.3s, color 0.3s;
}

.nav-links a:hover {
background: #3b34a6;
border-radius: 5px;
color: #fff;
}

button::before {
content:"";
position: absolute;
background-color: #fff;
top: 0px;
left: 0;
right: 0;
bottom: 0px;
z-index: -1;
transform: scaleX(0);
transform-origin: left;
transition: all 0.8s;
/* Container */
.container {
width: 100%;
max-width: 700px;
margin: 50px auto;
}

/* Card Styling */
.card {
background: #ffffff;
color: #333;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
padding: 30px;
text-align: center;
}

button:hover::before {
transform: scaleX(1);
/* Button */
button {
padding: 10px 20px;
background-color: #4e45d5;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
border: solid 3px #4e45d5;
color: black;
background-color: #3b34a6;
}

/* Input Fields */
input {
padding: 10px;
width: 100%;
max-width: 300px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}

0 comments on commit aa2d1ae

Please sign in to comment.