Page 2 of 8 FirstFirst 1234 ... LastLast
Results 11 to 20 of 77

Thread: "Check the Number is Prime" in every Programming Language

  1. #11
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: "Check the Number is Prime" in every Programming Language

    Ruby
    Code:
    #!/usr/bin/env ruby
    
    class PrimeGen
        attr_reader :primes
    
        def initialize
            @primes = [2, 3]
        end
    
        def generate_next
            def check_prime(n)
                limit = Math.sqrt(n).ceil
                @primes.each {|p| break false if n % p == 0; break true unless p < limit }
            end
    
            n = @primes[-1] + 2
            n += 2 until check_prime(n)
            @primes << n
            return n
        end
    end
    
    print "Enter a number: "
    num = gets.to_i
    
    p = PrimeGen.new
    p.generate_next until p.primes[-1] >= num
    
    if num == 2 or p.primes[-1] == num then puts "#{num} is prime" else puts "#{num} is composite" end
    Partly copied and pasted from one of my Project Euler programs.
    Last edited by jespdj; October 7th, 2008 at 02:56 PM.
    Ubuntu 12.04

  2. #12
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: "Check the Number is Prime" in every Programming Language

    Befunge

    Code:
    25v v p 05 +1g 05 <
    v0<       >: 50 g%|
    >p>&>:50g`|@,,"!p"<
    @ , "p"   <
    Gotta love those esoteric languages (and yes, I did write this code myself).

  3. #13
    Join Date
    Jul 2008
    Beans
    1,491

    Re: "Check the Number is Prime" in every Programming Language

    Quote Originally Posted by Anurag_panda View Post
    My Entries:
    C
    Code:
    #include<stdio.h>
    
    int main()
    {
    	int div;
    	int num;
    	
    	printf("Enter a number: ");
    	scanf("%d", &num);
    	
    	
    	for(div=2; div<=num/2; div++)
    	{
    		if(num%div==0)
    		{
    			printf("\nThe Number %d is not a prime\n", num);
    			break;
    		}
    	}
    	
    	if(!(div<num/2))
    		printf("\nThe Number %d is a prime\n", num);
    		
    	return 0;
    }
    C++
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int div;
    	int num;
    	
    	cout<<"Enter a number: ";
    	cin>>num;
    	
    	
    	for(div=2; div<=num/2; div++)
    	{
    		if(num%div==0)
    		{
    			cout<<"The number "<<num<<" is not a prime\n";
    			break;
    		}
    	}
    	
    	if(!(div<num/2))
    		cout<<"The number "<<num<<" is a prime\n";
    		
    	return 0;
    }
    I am a newbie in programming, particularly Python, so please highlight my mistakes and give tips.
    These entries are not 'valid' as they do report incorrect results:
    Code:
    prometheus@Bartix:~/Documents$ gcc -o test prime.c
    prometheus@Bartix:~/Documents$ chmod +x test
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 0
    
    The Number 0 is a prime
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 1
    
    The Number 1 is a prime
    Proved with your C version, but since you apply the same algorith and therefore the same bug to your C++ version; both will not work. The same thing can be shown for negative input. The reason is that you assume all primes will have been filtered out if and only if your for loop has been 'fully' executed, without any forced breaks. But the for loop will never be executed at all if the input is less than 2, yet your if-condition will dutifully ('div' having been set to 2, which is more than the input!) evaluate to true, and hence your program is tricked into believing anything less than 2 is prime.

  4. #14
    Join Date
    Dec 2006
    Location
    Hogwarts, `UNKNOWN`
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: "Check the Number is Prime" in every Programming Language

    Quote Originally Posted by Reiger View Post
    These entries are not 'valid' as they do report incorrect results:
    Code:
    prometheus@Bartix:~/Documents$ gcc -o test prime.c
    prometheus@Bartix:~/Documents$ chmod +x test
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 0
    
    The Number 0 is a prime
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 1
    
    The Number 1 is a prime
    Proved with your C version, but since you apply the same algorith and therefore the same bug to your C++ version; both will not work. The same thing can be shown for negative input. The reason is that you assume all primes will have been filtered out if and only if your for loop has been 'fully' executed, without any forced breaks. But the for loop will never be executed at all if the input is less than 2, yet your if-condition will dutifully ('div' having been set to 2, which is more than the input!) evaluate to true, and hence your program is tricked into believing anything less than 2 is prime.
    Thanks. Noted. Will Correct it.

    EDIT: Done. Is it OK Now?
    Last edited by Canis familiaris; October 7th, 2008 at 04:19 PM.

  5. #15
    Join Date
    Jul 2008
    Beans
    1,491

    Re: "Check the Number is Prime" in every Programming Language

    Ehrm no. As a matter of fact you have now made it explicit that your program should believe everything below 2 is prime:
    Code:
    if((!(div<num/2)) || num < 2)
    Whereas anything below 2 cannot possibly be prime, so I think you meant:
    Code:
    if(num>1 && (!(div<num/2)))
    EDIT: And there's more:
    Code:
    prometheus@Bartix:~/Documents$ gcc -o test prime.c
    prometheus@Bartix:~/Documents$ chmod +x test
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 4
    
    The Number 4 is not a prime
    
    The Number 4 is a prime
    Previously tested it with 27, which goes OK, but 4 is even so...

    Code:
    if(num>1 && (!(div<=num/2)))
    Should be the condition. Now for a minor touch up:
    Code:
    #include<stdio.h>
    
    int main()
    {
            int div;
            int num;
            
            printf("Enter a number: ");
            scanf("%d", &num);
            
            if(num>=2)
                    for(div=2; div<=num/2; div++)
                            if(num%div==0)
                                    break;
            
            if(num>1 && (!(div<=num/2)))
                    printf("\nThe Number %d is a prime\n", num);
            else
                    printf("\nThe Number %d is not a prime\n", num);
            
            return 0;
    }
    EDIT: The touch-up thing is ensuring that the program gives output in the rare (previously broken) cases. For instance, running the programatically-fixed version with input 0 wouldn't yield any output as no printf() call could be reached. So I restructured the code to make all non-primes fall into the else clause.
    Last edited by Reiger; October 7th, 2008 at 04:38 PM.

  6. #16
    Join Date
    Dec 2006
    Location
    Hogwarts, `UNKNOWN`
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: "Check the Number is Prime" in every Programming Language

    Quote Originally Posted by Reiger View Post
    Ehrm no. As a matter of fact you have now made it explicit that your program should believe everything below 2 is prime:
    Code:
    if((!(div<num/2)) || num < 2)
    Whereas anything below 2 cannot possibly be prime, so I think you meant:
    Code:
    if(num>1 && (!(div<num/2)))
    EDIT: And there's more:
    Code:
    prometheus@Bartix:~/Documents$ gcc -o test prime.c
    prometheus@Bartix:~/Documents$ chmod +x test
    prometheus@Bartix:~/Documents$ ./test
    Enter a number: 4
    
    The Number 4 is not a prime
    
    The Number 4 is a prime
    Previously tested it with 27, which goes OK, but 4 is even so...

    Code:
    if(num>1 && (!(div<=num/2)))
    Should be the condition. Now for a minor touch up:
    Code:
    #include<stdio.h>
    
    int main()
    {
            int div;
            int num;
            
            printf("Enter a number: ");
            scanf("%d", &num);
            
            if(num>=2)
                    for(div=2; div<=num/2; div++)
                            if(num%div==0)
                                    break;
            
            if(num>1 && (!(div<=num/2)))
                    printf("\nThe Number %d is a prime\n", num);
            else
                    printf("\nThe Number %d is not a prime\n", num);
            
            return 0;
    }
    EDIT: The touch-up thing is ensuring that the program gives output in the rare (previously broken) cases. For instance, running the programatically-fixed version with input 0 wouldn't yield any output as no printf() call could be reached. So I restructured the code to make all non-primes fall into the else clause.
    Thanks. I would also put your algorithm in C++ as well.

  7. #17
    Join Date
    Jul 2008
    Beans
    1,491

    Re: "Check the Number is Prime" in every Programming Language

    Well your C++ original refuse to compile: gcc spits out more gibberish than I can cope with, given I don't know C++. (I can read some of the code based on my experience with other, similar, languages; but I can't mkae heads or tails from what the GCC says...)
    Code:
    /tmp/ccCcmLVO.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    prime.cc:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
    prime.cc:(.text+0x59): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    prime.cc:(.text+0x97): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    prime.cc:(.text+0xdf): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    /tmp/ccCcmLVO.o: In function `__static_initialization_and_destruction_0(int, int)':
    prime.cc:(.text+0x129): undefined reference to `std::ios_base::Init::Init()'
    /tmp/ccCcmLVO.o: In function `__tcf_0':
    prime.cc:(.text+0x176): undefined reference to `std::ios_base::Init::~Init()'
    /tmp/ccCcmLVO.o: In function `main':
    prime.cc:(.text+0x199): undefined reference to `std::cout'
    prime.cc:(.text+0x19e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x1ac): undefined reference to `std::cin'
    prime.cc:(.text+0x1b1): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
    prime.cc:(.text+0x1e5): undefined reference to `std::cout'
    prime.cc:(.text+0x1ea): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x1f6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
    prime.cc:(.text+0x206): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x248): undefined reference to `std::cout'
    prime.cc:(.text+0x24d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x259): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
    prime.cc:(.text+0x269): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    /tmp/ccCcmLVO.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status

  8. #18
    Join Date
    Dec 2006
    Location
    Hogwarts, `UNKNOWN`
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: "Check the Number is Prime" in every Programming Language

    Quote Originally Posted by Reiger View Post
    Well your C++ original refuse to compile: gcc spits out more gibberish than I can cope with, given I don't know C++. (I can read some of the code based on my experience with other, similar, languages; but I can't mkae heads or tails from what the GCC says...)
    Code:
    /tmp/ccCcmLVO.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    prime.cc:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
    prime.cc:(.text+0x59): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    prime.cc:(.text+0x97): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    prime.cc:(.text+0xdf): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
    /tmp/ccCcmLVO.o: In function `__static_initialization_and_destruction_0(int, int)':
    prime.cc:(.text+0x129): undefined reference to `std::ios_base::Init::Init()'
    /tmp/ccCcmLVO.o: In function `__tcf_0':
    prime.cc:(.text+0x176): undefined reference to `std::ios_base::Init::~Init()'
    /tmp/ccCcmLVO.o: In function `main':
    prime.cc:(.text+0x199): undefined reference to `std::cout'
    prime.cc:(.text+0x19e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x1ac): undefined reference to `std::cin'
    prime.cc:(.text+0x1b1): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
    prime.cc:(.text+0x1e5): undefined reference to `std::cout'
    prime.cc:(.text+0x1ea): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x1f6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
    prime.cc:(.text+0x206): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x248): undefined reference to `std::cout'
    prime.cc:(.text+0x24d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    prime.cc:(.text+0x259): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
    prime.cc:(.text+0x269): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    /tmp/ccCcmLVO.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Use g++ to compile the C++ code. g++ is actually gcc called with special C++ libraries.

    Code:
    g++ filename -o program

  9. #19
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: "Check the Number is Prime" in every Programming Language

    Quote Originally Posted by Reiger View Post
    Well your C++ original refuse to compile: gcc spits out more gibberish than I can cope with, given I don't know C++. (I can read some of the code based on my experience with other, similar, languages; but I can't mkae heads or tails from what the GCC says...)
    That's not surprising! The command to compile C++ code is "g++".

  10. #20
    Join Date
    Jul 2008
    Beans
    1,491

    Re: "Check the Number is Prime" in every Programming Language

    Here you are:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
            int div;
            int num;
            
            cout<<"Enter a number: ";
            cin>>num;
            
            if(num>=2)
                    for(div=2; div<=num/2; div++)
                            if(num%div==0)
                                    break;
            
            if(num>1 && (!(div<=num/2)))
                    cout<<"The number "<<num<<" is a prime\n";
            else
                    cout<<"The number "<<num<<" is not a prime\n";
            
            return 0;
    }

Page 2 of 8 FirstFirst 1234 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •