so I just started programing like two days ago (c++) , and Im doing basic exercises, nothing really complicated, but for some reason I cant get this, any help is welcome
Pd: I have never been so scared of posting in a board
---------------------------------------
#include <iostream>
using namespace std;
int main()
{
int n;
int c;
int crash;
cout<<"Insert a number"<<endl;
cin>>n;
/*do{
if(n%c==0){
crash = 1;
}
c = c - 1;
}while(c == 1);*/
so I just started anal play like two days ago (c++) , and Im doing basic exercises, nothing really complicated, but for some reason I cant get this, any help is welcome
Pd: I have never been so scared of posting in a board
1. You only need to go from 2 to the square root of n.
2. Go to www.projecteuler.net. It has math-related exercises that will really make you think about how to write right code.
3. That's a pretty good first try.
#include <iostream>
using namespace std;
int main(){
int n; bool test = true;
cout << "Insert a number: "; cin >> n;
for(int c = 2; test || (c < n-1); c++){
if(n % c == 0) test = false;}
if(crash){ cout << "\nThe number is prime";}
else{ cout << "\nThe number is NOT prime";}
return 0;}
//IN SCHEME
(define (prime? x)
(define (ter a b)
(cond ((> b (- a 1)) 1)
((= (remainder a b) 0) 0)
((else (ter a (+ b 1)))))
(ter x 2))
It turns out dis.4chan.org/read/prog/1339746372/7 became specifically of use for me to improve my prime verifying function by handling rounded integer division so that I can use a / 2 instead of a - 1.
(define (r/ x y)
(define (iter a b c)
(cond ((< (- a b) 0) c)
(else (iter (- a b) b (+ c 1))))
(iter x y 0))
Also I prefer to use my own modulo function instead of (remainder, for the sake of reinventing the wheel more sloppily. (define (% x y)
(cond ((< (- a b) 0) a)
(else (% (- a b) b)))
Though I could alternatively design it in reference to my integer division function (define (% x y)
(- (/ x y) (r/ x y)))
Not sure which is better, and I'm sure it doesn't matter, but it's fun.