feat(problem): 42 - coded triangle numbers

This commit is contained in:
newt 2024-10-09 18:10:11 +01:00
parent 1c88037e21
commit 0a6586217b
4 changed files with 62 additions and 4 deletions

View file

@ -11,7 +11,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
## Challenge Completion
### <!-- completed -->17<!-- completed --> out of 100 public challenges completed.
### <!-- completed -->18<!-- completed --> out of 100 public challenges completed.
- [x] 1 - [Multiples of 3 or 5](src/bin/1.rs)
- [x] 2 - [Even Fibonacci numbers](src/bin/2.rs)
@ -54,7 +54,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
- [ ] 39 - Integer right triangles
- [ ] 40 - Champernowne's constant
- [ ] 41 - Pandigital prime
- [ ] 42 - Coded triangle numbers
- [x] 42 - [Coded triangle numbers](src/bin/42.rs)
- [ ] 43 - Sub-string divisibility
- [ ] 44 - Pentagon numbers
- [ ] 45 - Triangular, pentagonal, and hexagonal
@ -114,4 +114,4 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
- [ ] 99 - Largest exponential
- [ ] 100 - Arranged probability
<sub>Check out Project Euler <a href="https://projecteuler.net">here</a>.</sub>
<sub>Check out Project Euler <a href="https://projecteuler.net">here</a>.</sub>

1
resources/p042_words.txt Normal file

File diff suppressed because one or more lines are too long

View file

@ -24,7 +24,7 @@ fn name_score(name: String, position: usize) -> usize {
let mut letter_sum: usize = 0;
for char in name.chars() {
// Letters start in ASCII at 65
// Capital letters start in ASCII at 65
letter_sum += (char as usize) - 64;
}

57
src/bin/42.rs Normal file
View file

@ -0,0 +1,57 @@
/*
Problem 42 - Coded triangle numbers
The n th term of the sequence of triangle numbers is given by, t n = ½ n ( n +1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t 10 . If the word value is a triangle number then we shall call the word a triangle word.
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
*/
use regex::Regex;
use std::{fs, ops::Div};
fn read_words() -> Vec<String> {
return fs::read_to_string(euler::resources_path().join("p042_words.txt"))
.unwrap()
.split(",")
.map(|name| {
Regex::new("\"")
.unwrap()
.replace_all(name, "")
.to_string()
.to_lowercase()
})
.collect::<Vec<String>>();
}
fn is_triangle_number(number: usize) -> bool {
let solution = (((8 * number + 1) as f64).sqrt() - 1.0).div(2.0);
return solution.fract() == 0.0 && solution > 0.0;
}
fn count_triangle_words(words: Vec<String>) -> usize {
let mut count: usize = 0;
for word in words {
let mut sum = 0;
for char in word.chars() {
// Lowercase letters start in ASCII at 97
sum += (char as usize) - 96;
}
if is_triangle_number(sum) {
count += 1;
}
}
return count;
}
fn main() {
let words = read_words();
let count = count_triangle_words(words);
println!("Out of nearly 2000 common English words, {count} are triangle words!");
}