you have not defined p in your code.
Code:[which(p !=0)] Error in which(p != 0) : object 'p' not found
Hello, I am trying to generate the list ofusing simple loops..here is my code:
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?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)]
Last edited by murgesh; 10-17-2012 at 09:19 PM.
you have not defined p in your code.
Code:[which(p !=0)] Error in which(p != 0) : object 'p' not found
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
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
murgesh (10-17-2012)
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
murgesh (10-17-2012)
|
|