- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Copilot is a new addition to Windows 11, it is like ChatGPT, I just asked for a solution in Fortran to the Euler problem and got
program euler
implicit none
integer :: n, m, i, j, k, start, current, next
integer, dimension (:,:), allocatable :: adj
logical, dimension (:,:), allocatable :: visited
integer, dimension (:), allocatable :: path, degree
logical :: is_eulerian, is_semi_eulerian
! Read the number of vertices and edges
print *, 'Enter the number of vertices and edges:'
read *, n, m
! Allocate the arrays
allocate (adj(n,n), visited(n,n), path(m+1), degree(n))
! Read the adjacency matrix
print *, 'Enter the adjacency matrix:'
do i = 1, n
read *, (adj(i,j), j = 1, n)
end do
! Initialize the visited matrix and the degree array
visited = .false.
degree = 0
! Count the degrees of each vertex
do i = 1, n
do j = 1, n
degree(i) = degree(i) + adj(i,j)
end do
end do
! Check if the graph is Eulerian or semi-Eulerian
is_eulerian = .true.
is_semi_eulerian = .false.
k = 0
do i = 1, n
if (mod(degree(i), 2) /= 0) then
is_eulerian = .false.
k = k + 1
if (k == 1) then
start = i ! Save the first odd degree vertex as the start point
end if
end if
end do
if (k == 2) then
is_semi_eulerian = .true.
end if
! Print the result
if (is_eulerian) then
print *, 'The graph is Eulerian and has an Euler circuit.'
start = 1 ! Choose any vertex as the start point
else if (is_semi_eulerian) then
print *, 'The graph is semi-Eulerian and has an Euler path.'
else
print *, 'The graph is neither Eulerian nor semi-Eulerian and has no Euler path or circuit.'
stop
end if
! Find the Euler path or circuit
current = start
k = 1
path(k) = current
do while (k < m + 1)
do j = 1, n
if (adj(current,j) > 0 .and. .not. visited(current,j)) then
next = j ! Choose the next vertex
exit
end if
end do
visited(current,next) = .true. ! Mark the edge as visited
visited(next,current) = .true.
current = next ! Move to the next vertex
k = k + 1
path(k) = current ! Add the vertex to the path or circuit
end do
! Print the path or circuit
if (is_eulerian) then
print *, 'The Euler circuit is:'
else
print *, 'The Euler path is:'
end if
print *, (path(i), i = 1, m + 1)
! Deallocate the arrays
deallocate (adj, visited, path, degree)
end program euler
Soon humans will not write code?
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
solve 2nd order DE
program harmonic
implicit none
integer :: n, i
real :: h, x, y, z
real, dimension (:), allocatable :: xarr, yarr, zarr
! Read the number of steps and the step size
print *, 'Enter the number of steps and the step size:'
read *, n, h
! Allocate the arrays
allocate (xarr(n+1), yarr(n+1), zarr(n+1))
! Set the initial conditions
x = 0.0
y = 1.0
z = 0.0
! Store the initial values in the arrays
xarr(1) = x
yarr(1) = y
zarr(1) = z
! Loop over the steps
do i = 1, n
! Call the Runge-Kutta subroutine to update x, y, and z
call rk4sys(h, x, y, z)
! Store the updated values in the arrays
xarr(i+1) = x
yarr(i+1) = y
zarr(i+1) = z
end do
! Print the results
print *, 'The results are:'
do i = 1, n+1
print *, xarr(i), yarr(i), zarr(i)
end do
! Deallocate the arrays
deallocate (xarr, yarr, zarr)
contains
! Subroutine to implement the Runge-Kutta method (order 4) for systems of ODEs
subroutine rk4sys(h, x, y, z)
implicit none
real, intent (in) :: h
real, intent (inout) :: x, y, z
real :: k1, k2, k3, k4, l1, l2, l3, l4
! Calculate the coefficients
k1 = h * z
l1 = h * f(x, y, z)
k2 = h * (z + 0.5 * l1)
l2 = h * f(x + 0.5 * h, y + 0.5 * k1, z + 0.5 * l1)
k3 = h * (z + 0.5 * l2)
l3 = h * f(x + 0.5 * h, y + 0.5 * k2, z + 0.5 * l2)
k4 = h * (z + l3)
l4 = h * f(x + h, y + k3, z + l3)
! Update the values of x, y, and z
x = x + h
y = y + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0
z = z + (l1 + 2.0 * l2 + 2.0 * l3 + l4) / 6.0
end subroutine rk4sys
! Function to define the right hand side of the system of equations
function f(x, y, z)
implicit none
real, intent (in) :: x, y, z
real :: f
f = -y
end function f
end program harmonic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page