-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 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,7 @@ | ||
[package] | ||
name = "lc-0263-ugly-number" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
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,4 @@ | ||
|
||
# | ||
|
||
[问题描述](../problems/) |
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,40 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
pub fn is_ugly1(n: i32) -> bool { | ||
let mut n = n; | ||
while n != 0 && n % 2 == 0 { | ||
n /= 2; | ||
} | ||
while n != 0 && n % 3 == 0 { | ||
n /= 3; | ||
} | ||
while n != 0 && n % 5 == 0 { | ||
n /= 5; | ||
} | ||
n == 1 | ||
} | ||
|
||
pub type SolutionFn = fn(i32) -> bool; | ||
|
||
fn check_solution(func: SolutionFn) { | ||
assert!(func(6)); | ||
assert!(func(1)); | ||
assert!(!func(14)); | ||
assert!(!func(0)); | ||
} | ||
|
||
fn main() { | ||
check_solution(is_ugly1); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, is_ugly1}; | ||
|
||
#[test] | ||
fn test_is_ugly1() { | ||
check_solution(is_ugly1); | ||
} | ||
} |