9 Replies - 7822 Views - Last Post: 15 May 2009 - 09:43 PM Rate Topic: -----

#1 bigdski  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-May 09

finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 10:53 AM

i just started to learn mod and i was having trouble with this. heres my code:

Public Class Form1
	Dim num As Integer = 1
	Dim answer As Integer
	Dim x As Integer = 1
	Dim z As Integer
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Do Until x = 20
			num = 1 * x
			x = x + 1
			z = num * x
			If z Mod x = 0 Then
				z = answer
			End If
		Loop
		MsgBox(answer)
	End Sub
End Class



i have no idea what im doing wrong

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: finding the smallest number evenly divisible by 1-20

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 10:56 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 bigdski  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-May 09

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 02:14 PM

im trying to multiply 1-20 together and i was getting 380, then i used that mod and i got 0, i have no idea what im doing, i need to do it in a loop like what i did. if you can just give me some pointers i would really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#4 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 03:32 PM

From what I can see there (based on how your loop and variables are set up), you would be finding the largest number.

So you are trying to find the smallest number that can be evenly divided by all numbers 1 through 20? Am I correct? If so, I think I have another solution.

This post has been edited by Lumenii: 14 May 2009 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 03:39 PM

your looping is inverted - you want something like:

	Sub Main()

		Dim answer As Integer = 0
		Dim Found As Boolean
		Do
			' keep trying new possible answers
			answer += 1
			' assume it is the correct answer
			Found = True
			' test the assumption
			For i = 1 To 20
				If (answer Mod i) <> 0 Then
					' if fails - exit the for loop immediately
					Found = False
					Exit For
				End If
			Next
		Loop While Not Found
		' output your answer - hint it is big but < 32 bits - 232792560
		Console.WriteLine("Answer is : " & answer)
		Console.WriteLine("Enter to Finish...")
		Console.ReadLine()
	End Sub


This post has been edited by mark.bottomley: 14 May 2009 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#6 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 09:22 PM

I rewrote that code but I still can't get what it is supposed to do. Okay, example time.

Public Class Form1
	Dim num As Integer = 1
	Dim answer As Integer
	Dim x As Integer = 1
	Dim z As Integer
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Do Until x = 20
			num = x
			z = num * x
			If z Mod x = 0 Then
				answer = z
			End If
			x += 1
		Loop
		MsgBox(answer)
	End Sub
End Class


Here's your code now. Ok, lets start from the top. So, the loop will be repeated until x=20. Next we assigned the same integer to another variable (which I really don't get). Then we multiply x (which in this case is 1 at the moment) with itself. The old code multiplied x with x +1, which is something I can't understand, even though I can't understand this either. Ok, next we check if the modulus is 0, that is correct, checking for even division is best done like that. Then I corrected the next sentence. I think you wanted to get z to the answer variable not the other way? In VB it goes like this:
Dim a As Integer = 3
Dim b As Integer = 4

a = b 'This makes a's value 4
b=a 'While this would make b's value 3


OK, please explain what the code is supposed to do in your next post.

EDIT: Oh, and as an answer to the post above mine, please use CODE tags. And if you realized what the topic starter wants to do, please explain to me as well. And if the number goes over 10 digits, it won't fit into a 32-bit integer.

This post has been edited by juunas: 14 May 2009 - 09:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 10:12 PM

The original design is totally out to lunch as in not fixable... It sets x and num equal and then multiplies them giving x squared which is always evenly divisible by x! so it always assignd the answer in each loop. The final output of the bogus code is 361 = 19x19 and sine the x +=1 is at the end, it never does any calculation for 20. (which would give 400! - still wrong)

My design above is to keep trying larger numbers until you find one that will be evenly divisible by all the numbers 1 to 20 inclusive.

The math behind the this to find the number is to multiply all the unique prime factor groups in 1 to 20 together.

1 - 1
2 - 1, 2
3 - 1, 3
4 - 2, 2
5 - 1, 5
6 - 1, 2, 3
7 - 1, 7
8 - 1, 2, 2, 2
9 - 1, 3, 3
10 - 1, 2, 5
11 - 1, 11
12 - 1, 2, 2, 3
13 - 1, 13
14 - 1, 2, 7
15 - 1, 3, 5
16 - 1, 2, 2, 2, 2
17 - 1, 17
18 - 1, 2, 3, 3
19 - 1, 19
20 - 1, 2, 2, 5

therefore the unique prime factor groups are:

From 1 -> 1
Add 2 -> 1, 2
Add 3 -> 1, 2, 3
Add 4 -> 1, 2, 2, 3
Add 5 -> 1, 2, 2, 3, 5
Add 6 -> (nothing added - 1, 2, 3 already exist)
Add 7 -> 1, 2, 2, 3, 5, 7
Add 8 -> 1, 2, 2, 2, 3, 5, 7
Add 9 -> 1, 2, 2, 2, 3, 3, 5, 7
Add 10 -> (nothing added)
Add 11 -> 1, 2, 2, 2, 3, 3, 5, 7, 11
Add 12 -> (nothing added)
Add 13 -> 1, 2, 2, 2, 3, 3, 5, 7, 11, 13
Add 14 -> (nothing added)
Add 15 -> (nothing added)
Add 16 -> 1, 2, 2, 2, 2, 3, 3, 5, 7, 11, 13
Add 17 -> 1, 2, 2, 2, 2, 3, 3, 5, 7, 11, 13, 17
Add 18 -> (nothing added)
Add 19 -> 1, 2, 2, 2, 2, 3, 3, 5, 7, 11, 13, 17, 19
Add 20 -> (nothing added)

Multiply -> 1x2x2x2x2x3x3x5x7x11x13x17x19 = 232,792,560

This post has been edited by mark.bottomley: 14 May 2009 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: finding the smallest number evenly divisible by 1-20

Posted 14 May 2009 - 10:37 PM

Ahh, now I get it. It was just so weirdly presented + I'm still half asleep :S
Was This Post Helpful? 0
  • +
  • -

#9 bigdski  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-May 09

Re: finding the smallest number evenly divisible by 1-20

Posted 15 May 2009 - 07:34 AM

im just trying to find the first number that numbers 1-20 are divisible in... i think i got the code... ill post what i got, well part of it.
for x=1 to 99999999
if num mod 1 =0 then
msgbox(num)
end if
next

i did the num mod 1-20 though, not just 1. i got a really high number though, not sure if right, 232792560
thanks for the help tho

This post has been edited by bigdski: 15 May 2009 - 07:35 AM

Was This Post Helpful? 1

#10 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: finding the smallest number evenly divisible by 1-20

Posted 15 May 2009 - 09:43 PM

If you read my post you will see that 232,792,560 is the correct number along with a mathematical derivation of the answer. The big question I have is how you can justify using 999,999,999 as your starting point.

The simple way to pick an upper bound is to multiply all the number 1 to 20 together as that by definition MUST be divisible by all those numbers. You can handle numbers greater than 32bits (~+/- 2 billion) if you want by using the Decimal type - 128bits long fixed point.
Was This Post Helpful? 1

Page 1 of 1