cofneverlivetotell
asked on
A few simple Questions
Hey Experts.
I need a few basic questions answered, so i may as well just put them all together.
1) Is there an easy way to set the content of variables p0 - p100 to "" rather than a long list?
2) i am currently using the following :
Do
Do While N Mod X(0) = 0
N = N / X(0)
p0 = X(0)
Exit Do
Loop
Loop Until N Mod X(0) <> 0
Do
Do While N Mod X(1) = 0
N = N / X(1)
p1 = X(1)
Exit Do
Loop
Loop Until N Mod X(1) <> 0
Is there a loop that will do this for p0 to p100 rather than a long list?
3 and last) Is there a function to put all primes into an array of X (at the minute i have X = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41) but this could go up to 10's of thousands.... and i havnt got that much time) the X(0), X(1) in question 2 refers to this, so they need to be compatible as such.
Please post if you want any more info, lots of points going here on simple questions!
Cheers
Cof
I need a few basic questions answered, so i may as well just put them all together.
1) Is there an easy way to set the content of variables p0 - p100 to "" rather than a long list?
2) i am currently using the following :
Do
Do While N Mod X(0) = 0
N = N / X(0)
p0 = X(0)
Exit Do
Loop
Loop Until N Mod X(0) <> 0
Do
Do While N Mod X(1) = 0
N = N / X(1)
p1 = X(1)
Exit Do
Loop
Loop Until N Mod X(1) <> 0
Is there a loop that will do this for p0 to p100 rather than a long list?
3 and last) Is there a function to put all primes into an array of X (at the minute i have X = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41) but this could go up to 10's of thousands.... and i havnt got that much time) the X(0), X(1) in question 2 refers to this, so they need to be compatible as such.
Please post if you want any more info, lots of points going here on simple questions!
Cheers
Cof
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Uhm - this question doesnt seem to be showing up in the open questions.... What should i do
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Uhm - this question doesnt seem to be showing up in the open questions.... What should i do
Are you looking in the list of your questions?
Are you in the correct forum?
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic
mlmcc
Are you looking in the list of your questions?
Are you in the correct forum?
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic
mlmcc
question 1, you could try
for i = 0 to 100
Do
Do While N Mod X(i) = 0
N = N / X(i)
p1 = X(i)
Exit Do
Loop
Loop Until N Mod X(i) <> 0
next
for i = 0 to 100
Do
Do While N Mod X(i) = 0
N = N / X(i)
p1 = X(i)
Exit Do
Loop
Loop Until N Mod X(i) <> 0
next
make that question 2 sorry
would have to declare i as
Dim i as string
Dim i as string
ASKER
mlmcc I asked in CS about this, apparently your own questions dont show up in "questions awaiting answers"... just a little confusion
Mr Manderson, i will work that into my program in a few hours (not at home right now) but it looks feasable
Question 1 and 3 are successfully answered, so please nobody post about them :)
Cheers
Cof
Mr Manderson, i will work that into my program in a few hours (not at home right now) but it looks feasable
Question 1 and 3 are successfully answered, so please nobody post about them :)
Cheers
Cof
Questiion 2 - Nothing I am aware of. Is there a reason you used P0 - P100 rather than an array?
Dim P(100) as integer
Now you can do what you want quite easily. I realize the code change may be larger than writing the list.
Another thought would be to use a subroutine for the code and call it with
Call MySub(P0)
Call MySub(P1)
etc
mlmcc
Dim P(100) as integer
Now you can do what you want quite easily. I realize the code change may be larger than writing the list.
Another thought would be to use a subroutine for the code and call it with
Call MySub(P0)
Call MySub(P1)
etc
mlmcc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dim i as String gives a type mismatch with "For i = 0 to 100"
and "Loop Until" says there is no Do.
Cof
and "Loop Until" says there is no Do.
Cof
What is it you are trying to do with the code, can you show me. I dont know what x is for, only error i get for it is that x is not defined.
can try this
Dim i As Long
For i = 0 To 100
Do
Do While N Mod x(i) = 0
N = N / x(i)
p1 = x(i)
Exit Do
Loop
Loop Until N Mod x(i) <> 0
Next
can try this
Dim i As Long
For i = 0 To 100
Do
Do While N Mod x(i) = 0
N = N / x(i)
p1 = x(i)
Exit Do
Loop
Loop Until N Mod x(i) <> 0
Next
ASKER
Ok, here is my complete code:
' Currently only works for up to 4 prime factors
Option Explicit
Dim pctr As Long
Dim max_number As Integer
Dim N As Integer
Dim X(1000) As Variant
Dim Z As Integer
Dim p(1 To 1000) As String
Dim aindex As Integer
Private Sub PopulateArray()
aindex = 0
For pctr = 1 To max_number
If IsPrime(pctr) Then
X(aindex) = pctr
aindex = aindex + 1
End If
Next pctr
End Sub
Function IsPrime(lngNumber As Long) As Integer
Dim lngFactor As Long
IsPrime = True
lngFactor = Int(Sqr(lngNumber))
If lngNumber > 3 Then
If lngNumber Mod 2 = 0 Then
IsPrime = False
Else
For lngFactor = 3 To lngFactor Step 2
If lngNumber Mod lngFactor = 0 Then
IsPrime = False
Exit For
End If
Next
End If
End If
End Function
Private Sub Command1_Click()
Erase p
max_number = Text1.Text
Z = Text1.Text
N = Z
PopulateArray
Dim i As Long
For i = 1 To 100
Do
Do While N Mod X(i) = 0
N = N / X(i)
p(i) = X(i)
Exit Do
Loop
Loop Until N Mod X(i) <> 0
Next
If p(1) <> "" Then
Text2.Text = Z * (1 - (1 / p(1)))
End If
If p2 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p2))
End If
If p3 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p3))
End If
If p4 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p4))
End If
End Sub
My end goal of all this is to make a program that will give me the PHI of a number the user enters, and that it will generate primes itself and work out + display the prime factors as well as the answer.... i have a Java Script version of this (well it gives the PHI, but doesnt work out primes etc) and i am trying to convert it into a fast and efficient VB program)
Here is the JS code incase it illustrates the end goal a little better:
<script type="text/javascript">
function phi(n){
facts=0
for (i=1;i<n;i++){
common=1
for (j=2;j<=i;j++)
if ((i%j==0) && (n%j==0)) common=0
facts+=common
}
return facts
}
i = 2
do
{
document.write("PHI")
document.write(i)
document.write(" = ")
document.write(phi(i))
document.write("<br/>")
i++
}
while (i <= 1000)
</script>
' Currently only works for up to 4 prime factors
Option Explicit
Dim pctr As Long
Dim max_number As Integer
Dim N As Integer
Dim X(1000) As Variant
Dim Z As Integer
Dim p(1 To 1000) As String
Dim aindex As Integer
Private Sub PopulateArray()
aindex = 0
For pctr = 1 To max_number
If IsPrime(pctr) Then
X(aindex) = pctr
aindex = aindex + 1
End If
Next pctr
End Sub
Function IsPrime(lngNumber As Long) As Integer
Dim lngFactor As Long
IsPrime = True
lngFactor = Int(Sqr(lngNumber))
If lngNumber > 3 Then
If lngNumber Mod 2 = 0 Then
IsPrime = False
Else
For lngFactor = 3 To lngFactor Step 2
If lngNumber Mod lngFactor = 0 Then
IsPrime = False
Exit For
End If
Next
End If
End If
End Function
Private Sub Command1_Click()
Erase p
max_number = Text1.Text
Z = Text1.Text
N = Z
PopulateArray
Dim i As Long
For i = 1 To 100
Do
Do While N Mod X(i) = 0
N = N / X(i)
p(i) = X(i)
Exit Do
Loop
Loop Until N Mod X(i) <> 0
Next
If p(1) <> "" Then
Text2.Text = Z * (1 - (1 / p(1)))
End If
If p2 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p2))
End If
If p3 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p3))
End If
If p4 <> "" Then
Text2.Text = Text2.Text * (1 - (1 / p4))
End If
End Sub
My end goal of all this is to make a program that will give me the PHI of a number the user enters, and that it will generate primes itself and work out + display the prime factors as well as the answer.... i have a Java Script version of this (well it gives the PHI, but doesnt work out primes etc) and i am trying to convert it into a fast and efficient VB program)
Here is the JS code incase it illustrates the end goal a little better:
<script type="text/javascript">
function phi(n){
facts=0
for (i=1;i<n;i++){
common=1
for (j=2;j<=i;j++)
if ((i%j==0) && (n%j==0)) common=0
facts+=common
}
return facts
}
i = 2
do
{
document.write("PHI")
document.write(i)
document.write(" = ")
document.write(phi(i))
document.write("<br/>")
i++
}
while (i <= 1000)
</script>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Still not working, VB freezes when i try to run. Im just going to accept this question and be done with it, no time to work on further as i am getting a lot of other work in school at the minute and exams are in 2 weeks.
Thanks for your help anyway
Cheers
Cof
Thanks for your help anyway
Cheers
Cof
Glad i could help
mlmcc
mlmcc
ASKER