Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-15-2006, 01:00 AM   #1 (permalink)
 
Ultra Techie

Join Date: Sep 2005

Posts: 638

tommyboy123x is on a distinguished road

Send a message via AIM to tommyboy123x
Lightbulb From Python to C...

i need to "translate" from python to C. At least i think its Python..... I guess i don't want a complete answer because that would ruin the fun of programming . I do, however, want to know what "mod" means in Python as well as a little help here an there in this code:

Code:
limit = 1000000 // arbitrary search limit
for (n = 5; n <= limit; n++) {
  is_prime[n] = false // initialize the sieve
}

// put in candidate primes: 
// integers which have an odd number of representations by certain quadratic forms
for (x = 1; x <= sqrt (limit); x++) {
  for (y = 1; y <= sqrt (limit); y++) {
    n = 4x^2+y^2
    if n <= limit and n mod 12 == 1 or 5 then is_prime[n] = not (is_prime[n])
    n = 3x^2+y^2
    if n <= limit and n mod 12 == 7 then is_prime[n] = not (is_prime[n])
    n = 3x^2-y^2
    if x > y and n <= limit and n mod 12 == 11 then is_prime[n] = not (is_prime[n])
  }
}

// eliminate composites by sieving
for (n = 5; n <= sqrt (limit); n++) {
  if is_prime[n] then {
     // n is prime, omit multiples of its square; this is sufficient because 
     // composites which managed to get on the list cannot be square-free
     for (k = n^2; k <= limit; k += n^2) {
        is_prime[k] = false
     }
  }
}

print 2, 3
for (n = 5; n <= limit; n++) {
  if is_prime[n] then print n
}
this genereates prime numbers, and i just want to change it to C as easily as possible. So first off, what is the "mod" that is in bold above?

also, how can i make this start at, lets say, 100 instead of 5? do i just make n equal to something else?

I understand that with translating code, you will lose some effieciency and stuff, but i just want to map out how this script works and write it in C. However im sure im going to need to post here again for some help on something as well

thanks!
__________________

Some real (as in actual) surveys that pay money!
tommyboy123x is offline  
Old 05-17-2006, 12:12 AM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2005

Posts: 638

tommyboy123x is on a distinguished road

Send a message via AIM to tommyboy123x
Default

that is python...... right?

or is it something else?? (if so what is it, and still... what is the mod? )
__________________

Some real (as in actual) surveys that pay money!
tommyboy123x is offline  
Old 05-17-2006, 01:20 AM   #3 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

I don't know python, but to answer your question about mod:
x mod y returns the remainder (not the quotient!) of x/y (that is, x divided by y). The modulus operator in C is the percentage sign, %.
jaeusm is offline  
Old 05-17-2006, 02:00 AM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2005

Posts: 638

tommyboy123x is on a distinguished road

Send a message via AIM to tommyboy123x
Default

ok, that makes sense.... thanks

the algorithm works by using a remainder of 60, and then if it is in "list 1" then it is not a prime number, etc.

I think i have it from here, thank you!
__________________

Some real (as in actual) surveys that pay money!
tommyboy123x is offline  
Old 05-18-2006, 09:19 PM   #5 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

eh, that's not python..

Read the about in your compiler, seems like a generic C-based language, not sure what that is exactly.
__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
Old 05-22-2006, 09:37 PM   #6 (permalink)
 
Ultra Techie

Join Date: Sep 2005

Posts: 638

tommyboy123x is on a distinguished road

Send a message via AIM to tommyboy123x
Default

yeah, no, i realized that too... just a few days after posting, but i didnt bother to edit my posts

but yea, it still has to do with the remainder
__________________

Some real (as in actual) surveys that pay money!
tommyboy123x is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On