Week 3: Text
How many palindromic integer numbers less than 1000 are there?
A palindromic number reads the same backwards as forwards, for example 131, 55 and 9.
Hint: Use the reversing code in week 1 to help.Challenge 2012-10 – Math Challenge 1
Find all the numbers palindromic numbers less than 1000, eg. 1 131, 424 etc (the same forwards as backwards).
Quanti sono i numeri interi palindromi minori di 1000?
Un numero palindromo si legge allo stesso modo da sinistra a destra e da destra a sinistra, per esempio 131, 55 e 9.
Suggerimento: utilizza il codice invertente del problema Week 1 – 1.
Trova tutti i numeri palindromi minori di 1000, cioè 1, 131, 424 …
Soluzione 1
contatore=0
For numero=1 To 1000
Controlla()
EndFor
TextWindow.WriteLine("")
TextWindow.WriteLine("Sono " + contatore + " numeri.")
Sub Controlla
input =numero
output=""
For i=Text.GetLength(input) To 1 Step -1
output=Text.Append(output, Text.GetSubText(input, i, 1))
EndFor
If input = output Then
contatore=contatore+1
TextWindow.Write(input + " ")
EndIf
EndSub
Soluzione 2
Il sottoprogramma Controlla() fa a meno del numero rovesciato…
contatore=0
For numero=1 To 1000
Controlla()
EndFor
TextWindow.WriteLine("")
TextWindow.WriteLine("Sono " + contatore + " numeri.")
Sub Controlla
n=Text.GetLength(numero)
For i=1 to n/2
If(Text.GetSubText(numero, i, 1) <> Text.GetSubText(numero, n-i+1, 1)) Then
Goto Esci
EndIf
EndFor
contatore=contatore+1
TextWindow.Write(numero + " ")
Esci:
EndSub