Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Prime Palindromes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
illu45




PostPosted: Thu Apr 14, 2005 6:49 pm   Post subject: Prime Palindromes

Okay, so... here's the question

there's a number between 1 and 399, and you have to check whether or not its a prime palindrome or not. Basically what you have to do is a few things:

1) check it the number is prime
2) flip the number over (123 becomes 321)
3) check to see that the flipped number is prime
4) if it is, the number is a prime palindrome...

Here is my code so far... I thik there is an error in my prime checking, but I can't figure out what it is:

code:

var prime1, prime2 : boolean
var counter, number2 : int := 0
var numdiv : int
for number : 3 .. 399
    numdiv := number
    for count : 2 .. (number - 1) %Checking if the number is prime by making sure it has no factors other than 1 and itself
        if number / count mod 1 = 0 then
            prime1 := false
        else
            prime1 := true
        end if
    end for
    loop %Calculating the amount of digits in the number
        exit when numdiv <= 0
        numdiv := numdiv div 10
        counter := counter + 1
    end loop
    var digits : array 1 .. counter of int
    for count2 : 1 .. counter %Putting the digits into an array, one by one, each into its own slot
        digits (count2) := number div 10 ** (counter - 1)
    end for
    for decreasing count3 : counter .. 1
        number2 := number2 + digits (count3) * 10 ** (counter - 1) %Creating the backwards number
    end for
    if prime1 = true then
        for count4 : 2 .. (number2 - 1) %Checking if the backwards number is prime
            if number2 / count4 mod 1 = 0 then
                prime2 := false
            else
                prime2 := true
            end if
        end for
        if prime1 = true and prime2 = true then
            put number, " is a prime palindrome"
        end if
        counter := 0
    end if
end for
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Thu Apr 14, 2005 6:51 pm   Post subject: (No subject)

instead of making it so complicated, make to functions, one called checkprime and one called checkpalindrome, and just use intstr to turn the number into a string and check for palindrome
Martin




PostPosted: Thu Apr 14, 2005 6:51 pm   Post subject: (No subject)

Isn't this from usaco or something?
illu45




PostPosted: Thu Apr 14, 2005 6:51 pm   Post subject: (No subject)

martin wrote:
Isn't this from usaco or something?

Dunno... my teaher just gave it to me as lab 10b Very Happy..

P.S. Thanks for the quick replies, I'll try that
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: