[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# A few simple Questions

Posted on 2004-11-12
Medium Priority
181 Views
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.

Cheers
Cof
0
Question by:cofneverlivetotell
• 6
• 5
• 4
• +2

LVL 32

Assisted Solution

Erick37 earned 400 total points
ID: 12570211
If you declare p as:

Dim p(0 to 100) as string

The the Erase statement will reinit all members to ""

e.g.

Erase p
0

LVL 2

Author Comment

ID: 12570477
Ok thanks, any advance on other questions people?
0

LVL 2

Author Comment

ID: 12571148
Uhm - this question doesnt seem to be showing up in the open questions....  What should i do
0

LVL 76

Assisted Solution

David Lee earned 400 total points
ID: 12571208
Concerning the prime numbers question, have a look at this: http://www.vb-helper.com/howto_list_primes.html
0

LVL 101

Expert Comment

ID: 12576746
>>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?
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic

mlmcc
0

LVL 19

Expert Comment

ID: 12578294
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
0

LVL 19

Expert Comment

ID: 12578298
make that question 2 sorry
0

LVL 19

Expert Comment

ID: 12578302
would have to declare i as
Dim i as string
0

LVL 2

Author Comment

ID: 12578369

Mr Manderson, i will work that into my program in a few hours (not at home right now) but it looks feasable

Cheers
Cof
0

LVL 101

Expert Comment

ID: 12578383
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
0

LVL 19

Accepted Solution

Stephen Manderson earned 500 total points
ID: 12579504
Dim i As String

For i = 0 to 100
Do
Do While N Mod X(i) = 0
N = N / X(i)
p1 = X(i)
Exit Do
Loop Until N Mod X(i) <> 0
Loop
Next

That should work ok
0

LVL 2

Author Comment

ID: 12579821
Dim i as String gives a type mismatch with "For i = 0 to 100"

and "Loop Until" says there is no Do.

Cof
0

LVL 19

Expert Comment

ID: 12580530
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
0

LVL 2

Author Comment

ID: 12588816
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>
0

LVL 101

Assisted Solution

mlmcc earned 500 total points
ID: 12589835
You can't use lngfactor as the loop invariant and the final value. Also IsPrime returns True/False should be boolean

Function IsPrime(lngNumber As Long) As Boolean
Dim i as Long
Dim lngFactor As Long
IsPrime = True
lngFactor = Int(Sqr(lngNumber))
If lngNumber > 3 Then
If lngNumber Mod 2 = 0 Then
IsPrime = False
Else
For i = 3 To lngFactor Step 2
If lngNumber Mod i = 0 Then
IsPrime = False
Exit For
End If
Next i
End If
End If
End Function

mlmcc

0

LVL 2

Author Comment

ID: 12596111
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.

Cheers
Cof
0

LVL 101

Expert Comment

ID: 12600117

mlmcc
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture. Â  There is also a companion Debug Toolbar that looks like the followinâ€¦
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦
###### Suggested Courses
Course of the Month18 days, 4 hours left to enroll