• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sum of Digits

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really weird, Im sure my code is correct, donno why the result is wrong, its supposed to be a huge number in the end I guess, but Im always getting 262660

Problem:

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.



My CODE:
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is this...

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.



This ...



is more like... "Work out the sum of the ASCII values of all digits of the following one-hundred 50-digit numbers".

The problem never asked you to sum up the digits, it just asked you to ignore any digit higher than the 10th digit in the sum of the numbers. You also did a staight convert from char to num, which is the ASCII value, not the digit value (not that it matters as you weren't supposed to sum the digits anyway).

Henry
[ March 23, 2008: Message edited by: Henry Wong ]
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still did not get it
the problem, as I understand, wants the sum of the following one-hundred 50-digit numbers
but since the number is huge, the first ten digits are enough to prove a person solved the problem...
isn't this the problem?
if not, can you explain it more?
if I should not sum the digits up, then what should I do?

it just asked you to ignore any digit higher than the 10th digit in the sum of the numbers.



so I find the sum, and show the first ten digits then?

and this maybe better to solve the other problem

[ March 23, 2008: Message edited by: Sam Benry ]
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I still did not get it
the problem, as I understand, wants the sum of the following one-hundred 50-digit numbers
but since the number is huge, the first ten digits are enough to prove a person solved the problem...
isn't this the problem?



Yup, you described it perfectly here.


if I should not sum the digits up, then what should I do?



How did you go from "sum of the numbers" to "sum of the digits"?

For example, lets use 3 numbers instead of 100. Let's use 4 digits numbers instead of 50 digits. And lets look at the first 2 digits instead of 10.

The numbers are...

1234
1001
4532

The sum is 1234 + 1001 + 4532 = 6767. And the first 2 digits are 67.

What you did is... 1 + 2 + 3 + 4 + 1 + 0 + 0 + 1 + 4 + 5 + 3 + 2 = 26. How is this even close?

Henry
[ March 23, 2008: Message edited by: Henry Wong ]
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, problem solved
this is even easier than before...
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sam Benry:


You can omit the entire "add the int to an empty string" construction by using the String.valueOf methods. It's just a bit more efficient, gives the exact same results and also (IMHO) looks better.

If you are 100% sure these characters are all numbers, you can use the following code as well:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must sum the 100 numbers of 50 digits, are you able to solve this at this moment?, because I am on it.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,

It's generally considered polite to name the source of your question. In this case, you are talking about problem #13 of Project Euler, a fantastic place to get little 'puzzlers' to learn a new computing language.

In the future, please let everyone know where these problems come from.

Thanks
 
Marshal
Posts: 79938
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Jesus

We don't like to give out answers like that; please show us what you have already. And it might have been better to start a new thread; look at this deradfully-named FAQ!

Fred, I always get into disagreements about this, but there is nothing "little" about Project Euler tasks. They may take only a few lines to write, but as Sam Benry has demonstrated, they are *@*#$~ difficult!
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic