+ Reply to Thread
Results 1 to 8 of 8

Thread: Simple question about prime numbers

  1. #1
    Points: 833, Level: 15
    Level completed: 33%, Points required for next Level: 67

    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Simple question about prime numbers



    Hello, I am trying to generate the list of prime\;numbers using simple loops..here is my code:

    Code: 
    x<-integer(500)
    x[1]<-2
    x[2]<-3
    for (i in 3:500){
    x[i]=x[i-1]+2
    }
    
    k<-integer(500)
    k[1]<-2
    k[2]<-3
    k[3]<-5
    k[4]<-7
    i=3
    for (i in 5:500){
     if (x[i]%%2 != 0 && x[i]%%3 !=0 && x[i]%%5 !=0 && x[i]%%7 !=0) 
         k[i]<-x[i]     
    }
    k=k[which(k !=0)]
    k should give me all prime numbers, but there are non prime numbers in there as well. is it possible to get a list of primes by using the above method?
    Last edited by murgesh; 10-17-2012 at 09:19 PM.

  2. #2
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    Re: Simple question about prime numbers

    you have not defined p in your code.

    Code: 
    
    [which(p !=0)]
    Error in which(p != 0) : object 'p' not found

  3. #3
    Points: 833, Level: 15
    Level completed: 33%, Points required for next Level: 67

    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Simple question about prime numbers

    Quote Originally Posted by derksheng View Post
    you have not defined p in your code.

    Code: 
    
    [which(p !=0)]
    Error in which(p != 0) : object 'p' not found
    sorry that was supposed to be k. I misspecificied k and x s. Now I edited it as my original code is. At the end, when I try to see the vector k, all the numbers are not prime!

  4. #4
    Points: 833, Level: 15
    Level completed: 33%, Points required for next Level: 67

    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Simple question about prime numbers

    this is my final answer, but it does not entirely consist of prime numbers
    Code: 
    k
     [1]   2   3   5   7  11  13  17  19  23  29  31  37  41  43  47  53  59  61
     [19]  67  71  73  79  83  89  97 101 103 107 109 113 121 127 131 137 139 143
     [37] 149 151 157 163 167 169 173 179 181 187 191 193 197 199 209 211 221 223
     [55] 227 229 233 239 241 247 251 253 257 263 269 271 277 281 283 289 293 299
     [73] 307 311 313 317 319 323 331 337 341 347 349 353 359 361 367 373 377 379
     [91] 383 389 391 397 401 403 407 409 419 421 431 433 437 439 443 449 451 457
    [109] 461 463 467 473 479 481 487 491 493 499 503 509 517 521 523 527 529 533
    [127] 541 547 551 557 559 563 569 571 577 583 587 589 593 599 601 607 611 613
    [145] 617 619 629 631 641 643 647 649 653 659 661 667 671 673 677 683 689 691
    [163] 697 701 703 709 713 719 727 731 733 737 739 743 751 757 761 767 769 773
    [181] 779 781 787 793 797 799 803 809 811 817 821 823 827 829 839 841 851 853
    [199] 857 859 863 869 871 877 881 883 887 893 899 901 907 911 913 919 923 929
    [217] 937 941 943 947 949 953 961 967 971 977 979 983 989 991 997

  5. #5
    RotParaTon
    Points: 46,248, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,080
    Thanks
    211
    Thanked 1,608 Times in 1,378 Posts

    Re: Simple question about prime numbers

    It appears all you're doing is checking if the number is divisible by 2, 3, 5, or 7. And you want to check up to 1000. This clearly isn't going to work. So no... using the method you're trying to use it won't work.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  6. The Following User Says Thank You to Dason For This Useful Post:

    murgesh (10-17-2012)

  7. #6
    Points: 833, Level: 15
    Level completed: 33%, Points required for next Level: 67

    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Simple question about prime numbers

    Quote Originally Posted by Dason View Post
    It appears all you're doing is checking if the number is divisible by 2, 3, 5, or 7. And you want to check up to 1000. This clearly isn't going to work. So no... using the method you're trying to use it won't work.
    thank you.. looks like using a function will make this easier..but I wonder if prime numbers could still be generated using simple for and while loops.

  8. #7
    RotParaTon
    Points: 46,248, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,080
    Thanks
    211
    Thanked 1,608 Times in 1,378 Posts

    Re: Simple question about prime numbers

    Well yes you can. Just not using your method.

    There are a lot of different algorithms to find prime numbers. A relatively simple one is the Sieve of Eratosthenes. It's not too bad to program that.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  9. The Following User Says Thank You to Dason For This Useful Post:

    murgesh (10-17-2012)

  10. #8
    Points: 833, Level: 15
    Level completed: 33%, Points required for next Level: 67

    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Simple question about prime numbers


    Quote Originally Posted by Dason View Post
    Well yes you can. Just not using your method.

    There are a lot of different algorithms to find prime numbers. A relatively simple one is the Sieve of Eratosthenes. It's not too bad to program that.
    Thank you so much.. finally it worked..this is how i wrote!!
    Code: 
    x<-c(2:250)
    for (i in 2:250){
    for(j in 2:i){
    if (x[i]%%j==0)
    x[i]<-0
    }
    }
    x=x[which(x !=0)]
    x

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats