P42
projecteuler.net

Coded Triangle Numbers

ℹ️Published on Friday, 25th April 2003, 06:00 pm; Solved by 80950;
Difficulty rating: 5%

The $n$th term of the sequence of triangle numbers is given by, $t_n = \frac12n(n+1)$; so the first ten triangle numbers are: $$1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots$$

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?



Soluzione

Partiamo creando un insieme di numeri triangolari, per effettuare il futuro controllo sulla somma delle lettere delle parole.

triangles = [1/2*n*(n+1) for n in 1:20]
20-element Vector{Float64}:
   1.0
   3.0
   6.0
  10.0
  15.0
  21.0
  28.0
  36.0
  45.0
  55.0
  66.0
  78.0
  91.0
 105.0
 120.0
 136.0
 153.0
 171.0
 190.0
 210.0

L'idea è poi di processare il file, pulendolo per estrarre le singole parole, e fare un loop su ognuna costruendo la somma richiesta, ovvero mappando A come 1, B come 2, ecc. Questo lo si può anche ottenere shiftando in modo opportuno il codice ascii della singola lettera. Fatta la somma verifichiamo se è un numero triangolare e in tal caso incrementiamo la variabile del conteggio, che darà la risposta finale.

text = readlines(download("https://projecteuler.net/resources/documents/0042_words.txt"))[1]
clean_text = replace.(split(text,","),"\"" => "")
clean_text[1:10] # giusto un'occhiata
10-element Vector{String}:
 "A"
 "ABILITY"
 "ABLE"
 "ABOUT"
 "ABOVE"
 "ABSENCE"
 "ABSOLUTELY"
 "ACADEMIC"
 "ACCEPT"
 "ACCESS"
conteggio = 0
println("Triangular words:")
for w in clean_text
	tot = 0
	for c in w
		tot += Int(c)-64 # for upper case c
	end
	if tot in triangles
		println(w)
		global conteggio += 1
	end
end
Triangular words:
A
ABILITY
ABOVE
ACCOMPANY
ACHIEVEMENT
AGENCY
AGREE
AIR
ALREADY
AN
ANCIENT
APPARENT
APPOINT
APPROACH
ASSUME
AT
ATMOSPHERE
BAG
BAND
BANK
BAR
BEAT
BELONG
BENEATH
BONE
BOTH
BRIDGE
BUILDING
BURN
CALL
CAPACITY
CAREFUL
CASE
CHILD
CIVIL
CLOSELY
COME
CONFIDENCE
CONFIRM
CONSERVATIVE
CONSTRUCTION
CONTENT
COULD
CURRENTLY
DECISION
DEFINITION
DEMOCRATIC
DEPUTY
DESPITE
DISTINCTION
EAST
EDGE
EDUCATIONAL
EFFECT
EQUIPMENT
EVENT
FACE
FAIL
FAMILY
FEEL
FIELD
FIGURE
FLOOR
FREEDOM
FUND
FUTURE
GENTLEMAN
GREY
GROWTH
HAIR
HAPPY
HAVE
HERE
HIS
IF
INCIDENT
INCREASED
INCREASINGLY
INDIVIDUAL
INSTRUMENT
INTEND
INTENTION
IS
LAW
LEADER
LEAVE
LENGTH
LESS
LITTLE
LOVELY
MAN
MATCH
MERELY
MILK
MISTAKE
MOVE
MUCH
NEED
NOTICE
OBJECT
OBJECTIVE
OF
OIL
ONLY
OTHER
OURSELVES
PART
PASS
PATH
PERFORM
PRISON
PRIVATE
PROBABLY
PROCEDURE
QUALITY
QUESTION
RANGE
READ
REAL
RELIEF
REMOVE
REPRESENT
REQUEST
RESPOND
RIDE
SAMPLE
SAY
SEAT
SECURITY
SINGLE
SKY
SOIL
SOLICITOR
SONG
SOUTHERN
SPIRIT
START
SUGGESTION
TALL
TAX
THEORY
THREATEN
THROUGHOUT
TITLE
TOOTH
TOTALLY
TRAVEL
TYPE
UNABLE
UNDERSTAND
UPON
USE
VARIOUS
VARY
VIDEO
WAGE
WARM
WATCH
WE
WHILST
WIDELY
WOMAN
@show conteggio
conteggio = 162

Last modified: August 18, 2025. Website built with Franklin.jl and the lovely Julia programming language.