How to get the multiples of 3 or 5 in vb.net? - vb.net

How can I get the multiples of 3 or 5 in vb.net? I have this code but it gives me different output. Please help me.
Dim x As Integer
For x = 3 To 10
If x Mod 2 <> 0 Then
Dim sum As Integer
sum += x
MsgBox(x)
End If
Next
The output will be 3,5,7,9. The expected output should be 3,5,6,9. So any help?

x Mod 2 <> 0 gives you all numbers except numbers that are divisible by 2. But you want all numbers that are divisible by 3 or 5.
So this gives you the expected output:
For x = 3 To 9
If x Mod 3 = 0 OrElse x Mod 5 = 0 Then
' ... '
Note that my loops ends with 9 instead of 10 since 10 would be divisible by 5 but you dont expect it.

For Each i As Integer In Enumerable.Range(1,10) _
.Where(Function(i) i Mod 3 = 0 OrElse i Mod 5 = 0)
MsgBox(i)
Next i

Why do you check x Mod 2 <> 0, when you need multiplies of 3 and 5? Try following:
Dim x As Integer
For x = 3 To 10
If x Mod 3 = 0 OrElse x Mod 5 = 0 Then
Dim sum As Integer
sum += x
MsgBox(x)
End If
Next

Related

Is there a function to skip steps in a for loop?

I want to skip steps in a nested for loops of like 9 loops but the code will express my question and predicament:
Sub question()
For x = 0 To 10
For y = 0 To 10
If x = 5 And y = 7 Then
'skip to x = 8
End If
Next
Next
End Sub
Just set x = 8
Sub Question()
Dim x As Integer
Dim y As Integer
For x = 0 To 10
For y = 0 To 10
If x = 5 And y = 7 Then
x = 8
End If
Next
Next
End Sub

Excel VBA Loop x = x + 1

I'm new to Excel VBA and i'm trying to make a loop that sums X = X + 1 but when the loop ends it continues with the last X and doesn't starts again.
This is what I have:
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
Debug.Print K
Next K
Next I
This is what i get: 1 2 3 4 1 2 3 4 1 2 3 4 .
What i would like to get is: 1 2 3 4 5 6 7 8 9 10 11 12 .
Thanks for the help provided. I thought this would solve my problem but it's a bit more complicated. I need this because i'm adding coordinates in X, Y, Z format with this code:
For I = 1 To 6
X = 0
J = 10
RobApp.Project.Structure.Nodes.Create X = X + 1, 0, 0, J * (I - 1)
RobApp.Project.Structure.Nodes.Create X = X + 1, Range("N34") * 0.15, 0, J *
(I - 1)
Next I
"X = X+1" is the node number. I want it to be sequencial, 1,2,3,4 and so on while J is increasing in the Z coordinate. For example for the first line of code:
Node 1 = 0,0,0
Node 2 = 0,0,10
Node 3 = 0,0,20
and so on!
Or rather, use the extra variable X as you originally planned:
X = 0
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
X = X + 1
Debug.Print X
Next K
Next I

Project Euler 3

I have been trying to solve question 3 on project euler with the following vb code but I do not under stand why it is not working. Can someone please point me in the right direction?
Sub Main()
Dim p As Int64 = 600851475143
Dim y As Integer
For i As Int64 = p / 2 To 1 Step -1
If p Mod i = 0 Then
y = 0
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
y = y + 1
End If
Next
If y = 0 Then
Console.WriteLine(i)
Console.ReadLine()
End If
End If
Next
End Sub
The question is "The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
You start with n=1. Every number divides evenly by 1.
(so y = y+1 every time, and If y = 0 Then can never happen).

Combinations of 4-digit numbers whose individual digits sum to 5

I'm working on a scorecard at work which has columns representing 4 possible outcomes, for example:
Successful,
unsuccessful,
Exceptional,
Other
Each staff member is assessed 5 times in the month against those ratings. So 1 person might have 3 successful, 2 exceptional, 0 unsuccessful, 0 other.
So the max instances of each outcome is 5 but the total sum of instances can't be more than 5.
I could try to type out all the combinations (and get them wrong) - is there any function/formula/VBA that anyone knows of that will list out all of the possible combinations of outcomes for me?
e.g. 5000,4100,4010,4001,3200,3020,3002,3110,3011, etc...
Since your numbers can range from 0005 to 5000, you could just write a simple loop that tests each number to see if the digits total 5:
Sub GetPermutations()
Dim i As Long
For i = 5 To 5000
If SumDigits(i) = 5 Then Debug.Print Format$(i, "0000")
Next
End Sub
Function SumDigits(s As Variant) As Long
Dim i As Long
For i = 1 To Len(s)
SumDigits = SumDigits + CLng(Mid$(s, i, 1))
Next
End Function
Alternatively:
Dim w As Long, x As Long, y As Long, z As Long
For w = 0 To 5
For x = 0 To 5
For y = 0 To 5
For z = 0 To 5
If w + x + y + z = 5 Then Debug.Print w & x & y & z
Next
Next
Next
Next
c#:
// numbering 1 to 4 - i.e.
// 1 sucessful, 2 unsucessful, 3 exception, 4 other
for(int i=1;i<4;i++)
{
for(int n=1;n<4;n++)
{
for(int d=1;d<4;d++)
{
for(int q=1;q<4;q++)
{
if(i+d+n+q<=5)
Console.WriteLine(i+n+d+q+", ");
}
}
}
}
EDIT: just saw you asked for vba:
For number1 As Integer = 1 To 4 Step 1
For number2 As Integer = 1 To 4 Step 1
For number3 As Integer = 1 To 4 Step 1
For number4 As Integer = 1 To 4 Step 1
if(number1+number2+number3+number4<=5) Then
Debug.WriteLine(number1.ToString & number2.toString &number3.toString &number4.ToString & ",");
End If
Next number4
Next number3
Next number2
Next number1
The most simple solution I could think of, there's probably better though.
Thanks for your help everyone - i managed to use a combination of suggestions by starting with the number 1, auto-filling down to 5000, then "text to columns", fixed width to separate the digits, sum to 5, filter and hey presto! Seems to have produced the right number of possible combinations adding up to 5.
Thanks again for your help!

Visual Basic - 1-100 with numbers that are divisible by 2 and 3

The console seems to keep printing a repetition of the value '1'
Maybe this is because I am not incrementing the value correctly within the loop?
Or am I declaring the value of the variable in an incorrect location
Dim divx As Integer
Dim divy As Integer
divx = 1
Do While divx < 100
divy = divx Mod 2
If Not (divy = 0) Then
Console.WriteLine(divx)
Else
divx += 1
End If
Loop
Yes, you are not incrementing it, you can omit the Else:
Do While divx < 100
divy = divx Mod 2
If Not (divy = 0) Then
Console.WriteLine(divx)
End If
divx += 1
Loop
But if you also want to show numbers that are divisible by 3 you can modify your loop in this way:
Dim divx As Integer = 1
Do While divx < 100
Dim isDivisableBy2Or3 = divx Mod 2 = 0 OrElse divx Mod 3 = 0
If isDivisableBy2Or3 Then
Console.WriteLine(divx)
End If
divx += 1
Loop
If your goal is just to list out numbers that are divisible by 2 and 3, this should be the shortest route. Just trying to help :) If 2 or 3 then change And to Or.
For x As Integer = 1 To 100
If (x Mod 2 = 0) And (x Mod 3 = 0) Then Console.WriteLine(x)
Next x
If condition is 2 and 3 but not 5 then...
For x As Integer = 1 To 100
If (x Mod 2 = 0) And (x Mod 3 = 0) And Not (x Mod 5 = 0) Then Console.WriteLine(x)
Next x
Do While divx < 100
If Not (divx Mod 2 = 0) Then
Console.WriteLine("Divisible by 2: " & divx)
End If
If Not (divx Mod 3 = 0) Then
Console.WriteLine("Divisible by 3: " & divx)
End If
divx += 1
Loop