Project Euler 3 - vb.net

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).

Related

For Loop running error

This code doesn't find the correct output
for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)
if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?
Is there any limitation on the input n to run the for loop ?I would appreciate any help.
Function math(n As Integer) As Double
Dim i As Integer
Dim x As Double
For i = 1 To n - 1
x = (n - 1) * 2 + 5
sum = sum + x
Next i
math = sum
End Function
Why not simply:
Function math(n As Integer) As Double
Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function
???
if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense
In the for loop, if you don't precise the Step, the variable will only increment by 1.
And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :
Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double
Select Case n
Case Is > 1
For i = 1 To n - 1
x = (i - 1) * 2 + 5
Summ = Summ + x
Next i
Case Is = 1
Summ = (n - 1) * 2 + 5
Case Is = 0
Summ = 5
Case Else
MsgBox "This case is not supported", vbInformation + vbOKOnly
Exit Function
End Select
math = Summ
End Function
If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.

Randomly divide a whole number m into n parts such that the parts are whole numbers and each part lies between x and y

As an example. I want to randomly hand out 100 chocolates to 25 kids. I cannot give any kid more than 10 chocolates.
So here m = 100, n = 25, x = 1 and y = 12.
I have checked these questions.
Dividing a number into m parts uniformly randomly
Dividing a number into random unequal parts
They do give some idea but in these questions x and y are not specified.
So basically,
1) Total No. of Chocolates = 100
2) I can only give minimum 1 and maximum 12 chocolates to each kid
3) Chocolates should be distributed between 25 kids
4) I do not want any distribution (uniform or normal) - it should be purely random. (I am willing to exclude this condition if all else fails.)
Private Function divideUniformlyRandomly(n As Integer, m As Integer) As Integer()
Dim rRandom As New Random
Dim fences As Integer() = New Integer(m - 2) {}
For i As Integer = 0 To m - 3
fences(i) = rRandom.Next(0, n - 1)
Next
[Array].Sort(fences)
Dim result As Integer() = New Integer(m - 1) {}
result(0) = fences(0)
For i As Integer = 1 To m - 3
result(i) = fences(i + 1) - fences(i)
Next
result(m - 1) = n - 1 - fences(m - 2)
Return result
End Function
This does work but I get 0 and 13 as well. I cannot ensure x and y here.
Give each child x chocolate. This will leave you with m - (n * x) to distribute randomly. Keep distributing to children that have less than y chocolates, until there are no more chocolates.
Private Function divideUniformlyRandomly(n As Integer, m As Integer, x As Integer, y As Integer) As Integer()
Dim rRandom As New Random
Dim aResult As Integer() = New Integer(n - 1) {}
Dim i As Integer = 0
Dim remaining As Integer = m
' Every n must have a min of x.
For i = 0 To n - 1
aResult(i) = x
remaining -= x
Next
' distribute the remaining m over the children randomly
While remaining > 0
' pick a child randomly
i = rRandom.Next(0, n)
' if the child has less than y, give them one
If aResult(i) < y Then
aResult(i) += 1
remaining -= 1
End If
End While
' Debug
Dim sum As Integer = 0
For i = 0 To n - 1
Console.WriteLine("{0}: {1}", i, aResult(i))
sum += aResult(i)
Next
Console.WriteLine("Sum: {0}", sum)
divideUniformlyRandomly = aResult
End Function

what to do with fraction as index number

given these inputs x = 4, S = [1 2 3 4 5 6 7 8 9 10], and n = 10
search (x,S,n) {
i = 1
j = n
while i < j {
m = [(i+j)/2]
if x > Sm then i=n+1
else j = m
end
if x = Si then location = i
else location = 0
This code is not from any particular language its just from my discrete math hw, but I'm confused as to what Sm would equal on the first iteration because m would be 11/2. If i use a fraction as the index do I round down? Is there a general rule for this? Am I making any sense? Help pls

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

How to get the multiples of 3 or 5 in 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