Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

java- another problem


  • Please log in to reply

#1
jaj2

jaj2

    New Member

  • Member
  • Pip
  • 5 posts
Write a function that prints the sum of all the numbers from one to a million whose digits consist only of 2s and 9s. For example, 2, 9, 29, 92922, 222, and 99999 will be in your sum, but not 290, 942, or 4683.
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
please post the code you have so far.
hint: use long for sum instead of int

were you able to figure out the sum problem with 1s and 3s?

Edited by bdlt, 28 January 2006 - 02:24 PM.

  • 0

#3
jaj2

jaj2

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Thanks for the help, I was able to figure it out.
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
thanks for letting us know.

FYI -
last last number displayed was 499 993 500 021 - did you get this too?

the final sum(not displayed - add code to display it) should be 500 000 500 000

how do we know this final sum is correct?

for a sum from 1 to n of 1 + 2 + 3 + ... + n = n * (n + 1) / 2

multiply 1000000 * 1000001 then divide by 2 = 500 000 500 000
  • 0

#5
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

FYI -
last last number displayed was 499 993 500 021 - did you get this too?

the final sum(not displayed - add code to display it) should be 500 000 500 000

how do we know this final sum is correct?

for a sum from 1 to n of 1 + 2 + 3 + ... + n = n * (n + 1) / 2

multiply 1000000 * 1000001 then divide by 2 = 500 000 500 000

What? If we're looping one million times then how can the last number be greater than one million (499,993,500,021)? In the OP, it was stated that you can only use 9's and 2's. The last number will be 999,999. 500,000,500,000 is the sum of all numbers from 1 to 1000000 (inclusive)... this doesn't relate to the OP either. The sum I got was 41,169,513. Please verify this... I'm lost.
  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
instead of "last last number displayed" it would be more clear as "last sum displayed".

my error - "whose digits consist only of 2s and 9s". didn't 'read' 2s AND 9s(and misread examples 2, 9, 29,...). so I'll post a new 2s and 9s ONLY number.

"The sum I got was 41,169,513. " - think about it. the last 10 numbers summed will almost reach 10 million, so 41 million is way too low.

edit - it looks like there may be NO sums whose digits consist only of 2s and 9s. did you get this too?

Edited by bdlt, 04 February 2006 - 10:06 PM.

  • 0

#7
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

"The sum I got was 41,169,513. " - think about it. the last 10 numbers summed will almost reach 10 million, so 41 million is way too low.

So what'd you get?
  • 0

#8
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
this should be the sum: 500 000 500 000

and it looks like there are NO sums whose digits consist only of 2s and 9s. did you get this too?
  • 0

#9
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

this should be the sum: 500 000 500 000

How do you figure? As I said before, this is the sum of all numbers from 1 to 1000000.

and it looks like there are NO sums whose digits consist only of 2s and 9s. did you get this too?

What do you mean?
  • 0

#10
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts

for a sum from 1 to n of 1 + 2 + 3 + ... + n = n * (n + 1) / 2

multiply 1000000 * 1000001 then divide by 2 = 500 000 500 000



and it looks like there are NO sums whose digits consist only of 2s and 9s. did you get this too?

What do you mean?


there is not any sum that consists of only 2s and 9s.

1+2 = 3
1+2+3 = 6
1+2+3+4 = 10

1+2+3+4+5+6 = 21
1+2+3+4+5+6+7 = 28

and so on

as a test - try sums whose digits are only 0,1,2, and 9(I get 48)

what sums do you see that are only 2s and 9s?
  • 0

#11
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
I think we are interpreting the question differently. I summed up numbers 2, 9, 22, 29, 92, 99, etc. Reread the OP... I see how it could go both ways, but the last sentence states examples that must be in your final sum.
  • 0

#12
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
got it - the sum is only of numbers with 2s and 9s. we don't care what the sum of ALL numbers may be.

0 points for bdlt on this one.

edit - 41,169,513 looks correct

Edited by bdlt, 04 February 2006 - 10:56 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP