-9

So, first programming course, first nongraded assignment: In C++, for the range of numbers from 1 to 1 billion, find the sum of the numbers that are divisible (without remainder) by all 1 through 99. How do I go about this? How do I even find the lowest number divisible by 1:99?

edit: this isn't hw to be turned in, just something to think about. I would try some type of vectorizing in matlab, but this is my first day trying c++ so I really have no idea, I just learned how to initialize a variable.

Austin
  • 6,921
  • 12
  • 73
  • 138
  • 1
    1) loop 2) test loop values 3) make decisions based on test results – Marc B May 19 '15 at 20:24
  • What have you tried? What have you thought about? What ideas did you discard, and which ones do you think might work. Show us some sign that you did more than immediately post the question here? – Bill May 19 '15 at 20:25
  • Homework is pointless if you're not the one doing it. – Borgleader May 19 '15 at 20:26

1 Answers1

2
// In pseudocode a very basic algorithm:
main
  for i: 1 to 1000000000
    if (TestValue(i))
      Output(i)

TestValue(i)
  for j: 1 to 99
    if j does not divide i evenly
      return false
  return true

Of course, this won't be very performant. You might notice that if a number is evenly divisible by all numbers between 1 and 99, then it must be divisible by the set of prime factors in 1..99. For instance, in 1..19 the prime factors are: 2, 2, 2, 2, 3, 3, 5, 7, 11, 13, 17, 19. If something is evenly divisible by all numbers 1..19 then it must be evenly divisible by 2*2*2*2*3*3*5*7*11*13*17*19 = 232792560. To find all the numbers between 1 and 1000000000 that are evenly divisible by 1..19, I would find all the numbers between 1 and 1000000000 that are evenly divisible by 232792560 instead.

Bill
  • 14,257
  • 4
  • 43
  • 55
  • Funny you should mention 1..19. That number is already over 200 million. From 1..99 is well over 1 billion, leaving the range of numbers in the question. – chris May 19 '15 at 20:36
  • @chris: Yes! Before coding, it's a good idea to figure out the ranges of numbers we're talking about. In this case, knowing what the product of the prime factorization of 1..99 is will let us know whether it's < 1,000,000,000. – Bill May 19 '15 at 21:28