Added factor applet
This commit is contained in:
parent
2bce4afe7a
commit
829dad2c5f
@ -25,7 +25,7 @@ impl Cmd for Base32 {
|
|||||||
|
|
||||||
fn cli(&self) -> clap::Command {
|
fn cli(&self) -> clap::Command {
|
||||||
Command::new("base32")
|
Command::new("base32")
|
||||||
.author("The JeanG3nie <jeang3nie@hitchhiker-linux.org>")
|
.author("Nathan Fisher")
|
||||||
.about("Base32 encode/decode data and print to standard output")
|
.about("Base32 encode/decode data and print to standard output")
|
||||||
.args([
|
.args([
|
||||||
Arg::new("INPUT")
|
Arg::new("INPUT")
|
||||||
|
86
src/cmd/factor/mod.rs
Normal file
86
src/cmd/factor/mod.rs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
use clap::{Arg, ArgMatches, Command, value_parser};
|
||||||
|
use crate::Cmd;
|
||||||
|
use std::{error::Error, io::{self, BufRead}};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Factor {
|
||||||
|
name: &'static str,
|
||||||
|
path: Option<crate::Path>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const FACTOR: Factor = Factor {
|
||||||
|
name: "factor",
|
||||||
|
path: Some(crate::Path::UsrBin),
|
||||||
|
};
|
||||||
|
|
||||||
|
impl Cmd for Factor {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cli(&self) -> Command {
|
||||||
|
Command::new("factor")
|
||||||
|
.about("factor numbers")
|
||||||
|
.after_help(
|
||||||
|
"Print the prime factors of each specified integer NUMBER. If none are\n\
|
||||||
|
specified on the command line, read them from standard input."
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("number")
|
||||||
|
.help("the numbers to factor")
|
||||||
|
.num_args(0..)
|
||||||
|
.value_parser(value_parser!(u64))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> {
|
||||||
|
if let Some(matches) = matches {
|
||||||
|
match matches.get_many::<u64>("number") {
|
||||||
|
Some(numbers) => {
|
||||||
|
numbers.for_each(|n| print_factors(*n));
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
for line in io::stdin().lock().lines() {
|
||||||
|
for value in line.unwrap().split_whitespace() {
|
||||||
|
let num: u64 = value.parse()?;
|
||||||
|
print_factors(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path(&self) -> Option<crate::Path> {
|
||||||
|
self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_prime(num: u64) -> bool {
|
||||||
|
match num {
|
||||||
|
0 | 1 => false,
|
||||||
|
2 | 3 | 5 | 7 | 11 | 13 => true,
|
||||||
|
x if x % 2 == 0 => false,
|
||||||
|
_ => {
|
||||||
|
let mut x: u64 = 2;
|
||||||
|
while x < num / 2 {
|
||||||
|
if num % x == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
x += 1;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_factors(num: u64) {
|
||||||
|
print!("{num}:");
|
||||||
|
for n in 2..=num / 2 {
|
||||||
|
if num % n == 0 && is_prime(n) {
|
||||||
|
print!(" {n}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
@ -18,6 +18,7 @@ mod date;
|
|||||||
mod dd;
|
mod dd;
|
||||||
pub mod dirname;
|
pub mod dirname;
|
||||||
pub mod echo;
|
pub mod echo;
|
||||||
|
pub mod factor;
|
||||||
pub mod r#false;
|
pub mod r#false;
|
||||||
mod getty;
|
mod getty;
|
||||||
pub mod head;
|
pub mod head;
|
||||||
@ -41,6 +42,7 @@ pub use {
|
|||||||
bootstrap::{Bootstrap, BOOTSTRAP},
|
bootstrap::{Bootstrap, BOOTSTRAP},
|
||||||
dirname::{Dirname, DIRNAME},
|
dirname::{Dirname, DIRNAME},
|
||||||
echo::{Echo, ECHO},
|
echo::{Echo, ECHO},
|
||||||
|
factor::{Factor, FACTOR},
|
||||||
head::{Head, HEAD},
|
head::{Head, HEAD},
|
||||||
nologin::{Nologin, NOLOGIN},
|
nologin::{Nologin, NOLOGIN},
|
||||||
r#false::{False, FALSE},
|
r#false::{False, FALSE},
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::{Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE};
|
use super::{Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FACTOR, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE};
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
@ -35,6 +35,7 @@ impl Cmd for Shitbox {
|
|||||||
DIRNAME.cli(),
|
DIRNAME.cli(),
|
||||||
ECHO.cli(),
|
ECHO.cli(),
|
||||||
FALSE.cli(),
|
FALSE.cli(),
|
||||||
|
FACTOR.cli(),
|
||||||
HEAD.cli(),
|
HEAD.cli(),
|
||||||
NOLOGIN.cli(),
|
NOLOGIN.cli(),
|
||||||
HOSTNAME.cli(),
|
HOSTNAME.cli(),
|
||||||
@ -54,6 +55,7 @@ impl Cmd for Shitbox {
|
|||||||
Some(("bootstrap", matches)) => BOOTSTRAP.run(Some(matches))?,
|
Some(("bootstrap", matches)) => BOOTSTRAP.run(Some(matches))?,
|
||||||
Some(("dirname", matches)) => DIRNAME.run(Some(matches))?,
|
Some(("dirname", matches)) => DIRNAME.run(Some(matches))?,
|
||||||
Some(("echo", _matches)) => ECHO.run(None)?,
|
Some(("echo", _matches)) => ECHO.run(None)?,
|
||||||
|
Some(("factor", matches)) => FACTOR.run(Some(matches))?,
|
||||||
Some(("false", _matches)) => FALSE.run(None)?,
|
Some(("false", _matches)) => FALSE.run(None)?,
|
||||||
Some(("head", matches)) => HEAD.run(Some(matches))?,
|
Some(("head", matches)) => HEAD.run(Some(matches))?,
|
||||||
Some(("nologin", _matches)) => NOLOGIN.run(None)?,
|
Some(("nologin", _matches)) => NOLOGIN.run(None)?,
|
||||||
|
@ -3,7 +3,7 @@ use std::{env, error::Error, path::PathBuf, string::ToString};
|
|||||||
|
|
||||||
pub mod cmd;
|
pub mod cmd;
|
||||||
use cmd::{
|
use cmd::{
|
||||||
Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, HEAD, HOSTNAME, NOLOGIN, SHITBOX, SLEEP, TRUE,
|
Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, FACTOR, HEAD, HOSTNAME, NOLOGIN, SHITBOX, SLEEP, TRUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -39,7 +39,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|
|||||||
cmd::COMMANDS
|
cmd::COMMANDS
|
||||||
.set(Commands {
|
.set(Commands {
|
||||||
items: vec![
|
items: vec![
|
||||||
&BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &HEAD, &HOSTNAME, &NOLOGIN, &TRUE, &SLEEP,
|
&BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &FACTOR, &HEAD, &HOSTNAME, &NOLOGIN, &TRUE, &SLEEP,
|
||||||
&SHITBOX,
|
&SHITBOX,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user