mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-13 14:23:02 -05:00
feat: rename prime to random
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component, inject } from '@angular/core';
|
import { Component, inject } from '@angular/core';
|
||||||
import { PrimeService } from './prime-number.service';
|
import { HeavyCalculationService } from './heavy-calculation.service';
|
||||||
import { UnknownPersonComponent } from './unknown-person/unknown-person.component';
|
import { UnknownPersonComponent } from './unknown-person/unknown-person.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [CommonModule, UnknownPersonComponent],
|
imports: [CommonModule, UnknownPersonComponent],
|
||||||
providers: [PrimeService],
|
providers: [HeavyCalculationService],
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
template: `
|
template: `
|
||||||
<unknown-person [step]="loadingPercentage()" class="relative grow" />
|
<unknown-person [step]="loadingPercentage()" class="relative grow" />
|
||||||
@@ -22,11 +22,11 @@ import { UnknownPersonComponent } from './unknown-person/unknown-person.componen
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
primeService = inject(PrimeService);
|
private heavyCalculationService = inject(HeavyCalculationService);
|
||||||
|
|
||||||
loadingPercentage = this.primeService.loadingPercentage;
|
readonly loadingPercentage = this.heavyCalculationService.loadingPercentage;
|
||||||
|
|
||||||
discover() {
|
discover() {
|
||||||
this.primeService.calculatePrimeLength();
|
this.heavyCalculationService.startLoading();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
import { ApplicationConfig } from '@angular/core';
|
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
|
||||||
providers: [],
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { Injectable, computed, signal } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class HeavyCalculationService {
|
||||||
|
private finalLength = 664579;
|
||||||
|
private loadingLength = signal(0);
|
||||||
|
|
||||||
|
loadingPercentage = computed(
|
||||||
|
() => (this.loadingLength() * 100) / this.finalLength
|
||||||
|
);
|
||||||
|
|
||||||
|
startLoading() {
|
||||||
|
this.randomHeavyCalculalationFunction();
|
||||||
|
}
|
||||||
|
|
||||||
|
private randomHeavyCalculalationFunction() {
|
||||||
|
for (let num = 2; num <= 10000000; num++) {
|
||||||
|
let randomFlag = true;
|
||||||
|
for (let i = 2; i <= Math.sqrt(num); i++) {
|
||||||
|
if (num % i === 0) {
|
||||||
|
randomFlag = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (randomFlag) {
|
||||||
|
this.loadingLength.update((l) => l + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
import { Injectable, computed, signal } from '@angular/core';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class PrimeService {
|
|
||||||
private finalResult = 664579;
|
|
||||||
private primesLength = signal(0);
|
|
||||||
|
|
||||||
loadingPercentage = computed(
|
|
||||||
() => (this.primesLength() * 100) / this.finalResult
|
|
||||||
);
|
|
||||||
|
|
||||||
calculatePrimeLength() {
|
|
||||||
this.findPrimesUpToLimit(10000000);
|
|
||||||
}
|
|
||||||
|
|
||||||
private findPrimesUpToLimit(limit: number) {
|
|
||||||
for (let num = 2; num <= limit; num++) {
|
|
||||||
let isPrime = true;
|
|
||||||
for (let i = 2; i <= Math.sqrt(num); i++) {
|
|
||||||
if (num % i === 0) {
|
|
||||||
isPrime = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isPrime) {
|
|
||||||
this.primesLength.update((l) => l + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
import { bootstrapApplication } from '@angular/platform-browser';
|
import { bootstrapApplication } from '@angular/platform-browser';
|
||||||
import { appConfig } from './app/app.config';
|
|
||||||
import { AppComponent } from './app/app.component';
|
import { AppComponent } from './app/app.component';
|
||||||
|
|
||||||
bootstrapApplication(AppComponent, appConfig).catch((err) =>
|
bootstrapApplication(AppComponent).catch((err) => console.error(err));
|
||||||
console.error(err)
|
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user