*/
Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?
*/

View Phi\Form1.frm

Euler's totient function v1.0

Submitted By: islam_salah
Rating: (Not rated) (Rate It)


VERSION 5.00
Begin VB.Form Form1
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Phi (n) version 2.0  -  [[Email Removed]]"
   ClientHeight    =   4095
   ClientLeft      =   4995
   ClientTop       =   705
   ClientWidth     =   5385
   BeginProperty Font
      Name            =   "MS Sans Serif"
      Size            =   8.25
      Charset         =   178
      Weight          =   700
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   ScaleHeight     =   4095
   ScaleWidth      =   5385
   Begin VB.Frame Frame1
      Caption         =   "Calculate Phi (n) "
      Height          =   3915
      Left            =   60
      TabIndex        =   0
      Top             =   60
      Width           =   5235
      Begin VB.TextBox Text1
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   12
            Charset         =   178
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   405
         Left            =   1680
         TabIndex        =   2
         ToolTipText     =   "You can calculates phi(n) for number up to 2^31 ,In some times calculation might be slow if number more than 9 digits.  "
         Top             =   660
         Width           =   2295
      End
      Begin VB.CommandButton CmdPhi
         Caption         =   "Calculate Phi(n)"
         Height          =   555
         Left            =   1800
         TabIndex        =   1
         Top             =   3000
         Width           =   1635
      End
      Begin VB.Label Label2
         Alignment       =   2  'Center
         Caption         =   "n ="
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   12
            Charset         =   178
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   1140
         TabIndex        =   5
         Top             =   720
         Width           =   495
      End
      Begin VB.Label Lphi
         Alignment       =   2  'Center
         BackColor       =   &H8000000B&
         BorderStyle     =   1  'Fixed Single
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   12
            Charset         =   178
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00FF0000&
         Height          =   375
         Left            =   2460
         TabIndex        =   4
         ToolTipText     =   $"Form1.frx":1BEA
         Top             =   2040
         Width           =   2415
      End
      Begin VB.Label Label1
         Caption         =   "Phi(n)="
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   12
            Charset         =   178
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1560
         TabIndex        =   3
         Top             =   2100
         Width           =   855
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'Phi(n) version 2.0 program 2006
'by Islam Salah
'Egypt - Cairo
'E-mail : [[Email Removed]]
'Cell Phone : +20101723013
'Thanks for being thankfull


Private F As New Collection
Private PrimeNumArr As New Collection
Private m As Variant
Private j As Double

Private Sub CmdPhi_Click()
Dim aaa As Double

If IsNumeric(Text1.Text) Then

If CDbl(Text1.Text) < CDbl(2147483648#) And CDbl(Text1.Text) <> 0 Then

aaa = Factorize(CDbl(Text1.Text))

For i = 1 To j - 1

IsPrime (F(i))

Next

b = Phi(CDbl(Text1.Text))

Lphi.Caption = b

Else
MsgBox "Only check numbers up to:  2^31 (2147483648)  !"
End If

Else
MsgBox "Insert numbers only ! "
End If

Set F = Nothing
Set PrimeNumArr = Nothing

End Sub

Private Function IsPrime(num As Double) As Double

n = CDbl(num)

    For x = 2 To n - 1
   
       If (n Mod x) = 0 Then
   
   
'    Result = n / x
'    b = (InStr(CStr(Result), "."))
'    If b = 0 Then
'    MsgBox "Oops ! " + CStr(n) + " is not prime number !"
         
          Exit Function
        End If
   
    Next x
       
PrimeNumArr.Add (n)

m = m + 1
   

End Function

Private Sub Form_Load()
m = 1
Label1.Caption = "Phi(n)="

End Sub
Private Function Phi(n As Double) As Double

b = 1

For v = 1 To m - 1

a = (1 - (1 / F(v)))

b = b * a


Next v

b = b * n


Phi = b

m = 1
j = 0

End Function
Public Function Factorize(n As Double) As Double

    Dim lCount As Double

   
    j = 1
   
    lCount = 0

    ' Look for factors including 1 and the number itself.
    Dim i As Double
   
for1:    For i = 2 To n
        If (n Mod i) = 0 Then
       
            lCount = lCount + 1
           
                If i <> t Then
               
                F.Add (i)
               
                                           
                t = F(j)
               
                 DistinctCount = DistinctCount + 1
               
                 
               j = j + 1
               
                End If
               
         
            n = n / i
            i = 2
            Exit For
           
        End If
    Next
   
    If n = 1 Then
   
   Factorize = lCount
   
   Exit Function
   
    Else
   
    GoTo for1
   
    End If
   

End Function

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.