Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 517743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:51:56+00:00 2026-05-13T07:51:56+00:00

I have only learnt python for for few months and totally a newb in

  • 0

I have only learnt python for for few months and totally a newb in C, I got a C code from the web, and I am dying to study it. But i only understand python language, so would someone can help to translate the following code to python would be great. Thanks in advance!

for(i=0; i<n; i++) { /* Foreach particle "i" ... */
  ax=0.0;
  ay=0.0;
  az=0.0;
  for(j=0; j<n; j++) { /* Loop over all particles "j" */
    dx=x[j]-x[i];
    dy=y[j]-y[i];
    dz=yz[j]-z[i];
    invr = 1.0/sqrt(dx*dx + dy*dy + dz*dz + eps);
    invr3 = invr*invr*invr;
    f=m[j]*invr3;
    ax += f*dx; /* accumulate the acceleration from gravitational attraction */
    ay += f*dy;
    az += f*dx;
  }
  xnew[i] = x[i] + dt*vx[i] + 0.5*dt*dt*ax; /* update position of particle "i" */
  ynew[i] = y[i] + dt*vy[i] + 0.5*dt*dt*ay;
  znew[i] = z[i] + dt*vz[i] + 0.5*dt*dt*az;
  vx[i] += dt*ax; /* update velocity of particle "i" */
  vy[i] += dt*ay;
  vz[i] += dt*az;
}

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:51:57+00:00Added an answer on May 13, 2026 at 7:51 am

    Here’s a literal translation

    (Edit: was a literal translation but there are some anomalies in the original C code which @hughdbrown pointed out in comments — looks like you’re trying to study this subject from code so utterly broken it wouldn’t even compile, which seems very unwise on your part, so, anyway, I’m taking the liberty of fixing the apparent errors and absurdities of the original C code in this Python “almost”-literal translation…):

    import math
    
    for i in range(n):
      ax = ay = az = 0.0
      for j in range(n):
        dx=x[j]-x[i]
        dy=y[j]-y[i]
        dz=z[j]-z[i]
        invr = 1.0/math.sqrt(dx*dx + dy*dy + dz*dz + eps)
        f=m[j]*invr**3
        ax += f*dx  # accumulate the acceleration from gravitational attraction
        ay += f*dy
        az += f*dz
      xnew[i] = x[i] + dt*vx[i] + 0.5*dt*dt*ax
      ynew[i] = y[i] + dt*vy[i] + 0.5*dt*dt*ay
      znew[i] = z[i] + dt*vz[i] + 0.5*dt*dt*az
      vx[i] += dt*ax  # update velocity of particle "i"
      vy[i] += dt*ay
      vz[i] += dt*az
    

    Of course it assumes lists x, y, z, … vx, vy, vz are all pre-existing and of the same length n, just like the C code snippet assumes for the same-named arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I tested this solution in WebKit (Chrome/Safari), Firefox and IE… May 15, 2026 at 7:25 pm
  • Editorial Team
    Editorial Team added an answer $("option:contains('COUNTRY NAME HERE')").attr('selected','selected'); something like that? May 15, 2026 at 7:25 pm
  • Editorial Team
    Editorial Team added an answer As far as I see at the moment, the problem… May 15, 2026 at 7:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.