Dim p(0 to 100) as string

The the Erase statement will reinit all members to ""

e.g.

Erase p

Solved

Posted on 2004-11-12

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

17 Comments

Dim p(0 to 100) as string

The the Erase statement will reinit all members to ""

e.g.

Erase p

Are you looking in the list of your questions?

Are you in the correct forum?

http://www.experts-exchang

mlmcc

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

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

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

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

and "Loop Until" says there is no Do.

Cof

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

' 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>

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

Title | # Comments | Views | Activity |
---|---|---|---|

Instruction to open a Contact selected in a List Box in Outlook 2010 via VBA | 73 | 83 | |

VBA: Do Loop with Input Box | 14 | 38 | |

vba find the last empty column | 10 | 73 | |

I Need help preventing my button click event from being raised twice on my VB.Net application | 6 | 43 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**17** Experts available now in Live!