Solve #17: Number Letter Counts

  • Thread starter Arman777
  • Start date
In summary, the problem was that the programmer was trying to add a sum of a list of integers (K), but forgot to keep track of the indices (i). When K.append(P), len(K) increased in each append, but it should have been just stayed at 99. This was fixed by changing len(K) to 99.
  • #1
Arman777
Insights Author
Gold Member
2,168
192

Homework Statement


https://projecteuler.net/problem=17

Homework Equations

The Attempt at a Solution


Can someone point out where am I doing wrong ? I get some extra numbers but I didnt understand why

Python:
#!/usr/bin/python

A=["one","two","three","four","five","six","seven","eight","nine"]
B=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
C=["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]
D=["hundred","thousand","and"]
K=[]
for i in range(len(A)):
    y=len(A[i])
    K.append(y)
for i in range(len(B)):
    y=len(B[i])
    K.append(y)
L=[]
for i in range(8):
    for j in range(9):
        l=len(C[i]+A[j])
        L.append(l)
for i in range(len(C)):
    y=len(C[i])
    L.append(y)
K.extend(L)
l=sum(K)
for i in range(len(A)):
    p=len(D[0]+D[2]+A[i])*len(K)+l
    K.append(p)
for i in range(len(A)):
    y=len(D[0]+A[i])
    K.append(y)
K.append(len(A[0]+D[1]))
h=sum(K)
print(h)

I know that its current until K.extend(L) part then in the middle when I am trying to find the numbers 100-999 something goes wrong but I didnt undersstand why.
Thanks
 
Last edited:
Physics news on Phys.org
  • #2
I would put print statements throughout your code printing the name and value of each variable so that you can walk through a run and see what an where it went south or if you have a debugger use it to step through your code.
 
  • Like
Likes Arman777
  • #3
  • Like
Likes StoneTemplePython and Arman777
  • #4
Mark44 said:
@Arman777, since you're programming in Python, it comes with a simple debugger that would be useful to you. I wrote a couple of Insights articles a while back on this debugger.
https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-1/
https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-2/
Thanks, I ll look at them in detail.

I understand the problem. It's in these lines, very simple error actually.

Python:
for i in range(len(A)):
    p=len(K)*len(A[i]+D[0]+D[2])+l
    K.append(p)
when I say len(K) and then K.append(P), len(K) increases in each append (it goes like 99.100.101 etc ) but it should have been just stayed at 99. So I just changed len(K) with 99 and the problem solved.
 

1. How do you solve the number-letter count problem?

To solve the number-letter count problem, you need to first understand the instructions. The problem requires you to count the number of letters used if all the numbers from 1 to 1000 were written out in words. You can then use a programming language or a spreadsheet to automate the counting process.

2. What is the easiest way to solve this problem?

The easiest way to solve this problem is by using a programming language, such as Python or JavaScript. These languages have built-in functions and methods that can help you efficiently count the number of letters in a word or phrase.

3. What are some tips for solving this problem efficiently?

One tip for solving this problem efficiently is to break it down into smaller parts. For example, you can first count the number of letters in the words for the numbers 1 to 9, then for 10 to 19, and so on. Another tip is to use loops in your code to avoid repetitive tasks.

4. Can you solve this problem without using a computer?

Yes, you can solve this problem without using a computer. However, it may take longer and require more manual effort. One way to do this is by using a pen and paper to write out the numbers and count the letters for each number individually.

5. What other similar problems are there to this one?

There are many other problems that involve counting or manipulating numbers. Some examples include finding the sum of all the numbers from 1 to 100, counting the number of prime numbers within a certain range, and calculating the factorial of a given number.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
839
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • General Discussion
Replies
3
Views
2K
Back
Top