Jump to content

as green as it gets: code help

- - - - -

  • Please log in to reply
6 replies to this topic

#1
hoping

hoping

    Newbie

  • Members
  • Pip
  • 8 posts
This code isn't working as I would like it to . . . where'd I mess it up? I building up to trying to find primes. I am trying to test as I go and I can't figure out what I've done wrong here.

--FYI-I'm as green as they come. I've been "programming" for about two days now. Oh and I'm using python 2.5.4 (it's the version used by an online video series I am trying to get through-just started yesterday)

For formatting purposes I have inserted "-" for each tab as the forum is dropping the leading spaces


-pcand=6
-pcandsub=5
-Lprime=5
-count=3
-while count<10:
-- if pcand%2==0:
--- pcand=pcand+1
--- print ("pcand" +str(pcand))
-- if pcand%pcandsub>0:
--- pcandsub=pcandsub-1
--- print pcandsub
-- elif pcandsub>3:
--- pcandsub=pcandsub-1
-- elif pcandsub<3:
--- lprime=pcand
--- pcand=pcand+1
--- pcandsub=pcand-1
--- print lprime
--- count=count+1
--- print("count " +str(count))
--- pcandsub=pcandsub-1
--- if count==9:
---- print count

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
You could use the CODE tags, that way the leading spaces won't get dropped.

{CODE} and {/CODE}

except that '{' should actually be '[' and '}' should actually be ']' . I can't use the actuall '[' and ']' characters because then they would be turned into code; like this:
 and 

[ C O D E ]  a n d   [ / C O D E ] 

but without the extra spaces.

#3
hoping

hoping

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks Rhetorical

Please see the code below with the actual indents


pcand=6

pcandsub=5

Lprime=5

count=3

while count<10:

    if pcand%2==0:

        pcand=pcand+1

        print ("pcand" +str(pcand))

    if pcand%pcandsub>0:

        pcandsub=pcandsub-1

        print pcandsub

    elif pcandsub>3:

        pcandsub=pcandsub-1

    elif pcandsub<3:

        lprime=pcand

        pcand=pcand+1

        pcandsub=pcand-1

        print lprime

        count=count+1

        print("count " +str(count))

        pcandsub=pcandsub-1

        if count==9:

            print count



#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
To be honest, I don't know Python.

But, can you tell more about what the code is supposed to do and what the code actually does?

#5
hoping

hoping

    Newbie

  • Members
  • Pip
  • 8 posts
sure I am setting several variables
1) a variable that is tracking the current candidate for being a primenumber
2) the current number that is dividing into the candidate
3) the last prime found
4) a counter for how many primes have been found

Then I am trying to 1) remove even numbers before testing them completely
2) divide the prime candidate by the divider and determine if there is a remainder
--2a)If there is a remainder subtract 1 from the divisor
if there is no remainder and the divisor is greater than three subtract one from the divisor
if it less than three there are a series of updates to the variables
change the last found prime to the current candidate
add one to the candidate
made the new divisor the current new candidate less one
add one to the counter

There are several prints in there that are just there to tell me where it is in the process. They are not essential. In writing this I did see one error (put in an extra subtraction of one from the divisor in the last section), but it didn't fix it. The problem I am running into is that the program stops somewhere in the middle.I don't get an error it just stops and does not return to the command line.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
You forgot the 'else' :
pcand=6

pcandsub=5

Lprime=5

count=3

while count<10:

    if pcand%2==0:

        pcand= pcand+1

        print "pcand " + str(pcand) 

    if pcand % pcandsub > 0 : 

        pcandsub=pcandsub-1

        print pcandsub

    elif pcandsub>3:

        pcandsub=pcandsub-1

    elif pcandsub<3:

        lprime=pcand

        pcand=pcand+1

        pcandsub=pcand-1

        print lprime

        count=count+1

        print("count " +str(count))

        pcandsub=pcandsub-1

        if count==9:

            print count 

    else: 

        pcand += 1 

I'm not sure if that fixes everything, but at least now it doesn't just hang.

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Why not just use this algorithm ('#' means the rest of the line is a comment)?:
# 2 is the only even prime number; we'll just start from that. 

primes= [2] 

# The first candidate for a prime number would be 3. 

pcand= 3 


# We start the count from 0. 

count= 0 


while true 

	# We think that pcand might be a prime number, so we say it is. 

	is_prime= true 

	# Now the loop that would see if it's divisible by any prime number that we have so far. 

	for i in 0..(primes.length - 1)         # for (i= 0; i < primes.length; i += 1) 

		# primes.length is the number of elements in the array primes, since 

		# primes is an array. 

		if pcand % primes[i] != 0           # If remainder not 0 (if not divisible by primes[i]). 

			# If it's not divisible, then the modulus (remainder; %) 

			# would be non-zero. 

		else 

			# Else, it's a composite number. 

			# Any composite (non-prime) number is always divisible by at least one prime number. 

			# If we were able to divide it by primes[i], then it's composite, for sure. 

			is_prime= false 

			# There's no point of continuing if we know that it's not a prime number; 

			# we can just stop the loop now, because is_prime is not going to change 

			# anyway. 

			break 

		end 

		# When the loop starts, i is 0. Each time the loop continues, i is incremented (i= i + 1). 

		# The loop stops when i is no longer less than { the number of prime numbers so far - 1 }. 

		# i is the index into our array. primes is the array. primes[0] is 2. primes[1] is the 

		# next prime number after 2. primes[2] is the second prime number after 2. primes[3] is 

		# the third prime number after 2. primes[4] is the fourth prime number after 2. primes[i] 

		# is the ith prime number after 2. And so on. 

		# If the candidate is divisible by at least one of these prime numbers, the candidate is 

		# not a prime number, so we set is_prime to false. If we were unable to divide the 

		# candidate by any of the prime numbers so far, then we know it's a prime number 

		# and is_prime stays true (like we set it before this 'for' loop). 

	end 

	

	# If it's a prime number, add it to the list (add it to the primes array). 

	if is_prime == true 

		primes[primes.length]= pcand 

		# Also, increment the count, if it's prime. 

		count += 1 

	end 

	

	# Increment the candidate. 

	pcand += 1 

	

	# If count > 10, exit from the loop. 

	if count > 10 then break end 

end 


print primes 

It's in Ruby, so you would have to do a little Ruby-to-Python conversion; I did my best to comment on it. It works.


I attached the output:
Attached File  prime_output.png   88.64K   17 downloads




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users