|
|
On 13/01/2022 21:30, Cabesa wrote:
> Now, this is quite a simple request- but I ve been unable to do so myself and
> have no idea how to accomplish it.
> Let me explain:
>
> I have a ".txt" file, with a random number. The idea is to debug a message that
> states whether said number is prime or not. Now,this should be quite an easy
> task, but i dont seem to wrap my head around it and dont know witch "ifs" or
> "whiles" to put so that the programme knows if the number meets with the
> condition.
>
> Thanks in advance (for those that read this far)
>
It's not the best way to do it, but it's done in 5mn ... ;)
// -----------------------------------------------------------
// --- unitary test
// -----------------------------------------------------------
#declare num = 98452;
#declare cnt = 0;
#declare i=2;
#while(i<num)
#if (mod(num,i)=0)
#declare cnt=cnt+1;
#end
#declare i=i+1;
#end
#debug concat(str(num,0,0)," is ")
#if(cnt !=0 )
#debug "not a prime number\n"
#else
#debug "a prime number\n"
#end
#debug "\n\n------------\n\n"
// -----------------------------------------------------------
// --- find prime numbers between 0 and ...
// -----------------------------------------------------------
#declare num=0;
#while(num<50)
#declare cnt = 0;
#declare i=2;
#while( i < num)
#if (mod(num,i)=0)
#declare cnt=cnt+1;
#end
#declare i=i+1;
#end
#if(cnt =0 )
#debug concat("prime number ",str(num,0,0),"\n")
#end
#declare num=num+1;
#end
#debug "\n\n------------\n\n"
// -----------------------------------------------------------
// --- macro to test if a it is a prime numbers
// -----------------------------------------------------------
#macro IsPrime(n)
#local cnt = 0;
#local i=2;
#while(i<n)
#if(mod(n,i)=0)
#declare cnt=cnt+1;
#end
#declare i=i+1;
#end
(cnt=0)
#end
// -----------------------------------------------------------
// --- test a number using the macro
// -----------------------------------------------------------
#declare a = 51286;
#debug concat("Is ",str(a,0,0)," a prime number ? ")
#if(IsPrime(a))
#debug "yes\n"
#else
#debug "no\n"
#end
#declare a = 51287;
#debug concat("Is ",str(a,0,0)," a prime number ? ")
#if(IsPrime(a))
#debug "yes\n"
#else
#debug "no\n"
#end
#declare a = 51288;
#debug concat("Is ",str(a,0,0)," a prime number ? ")
#if(IsPrime(a))
#debug "yes\n"
#else
#debug "no\n"
#end
#debug "\n\n------------\n\n"
--
Kurtz le pirate
Compagnie de la Banquise
Post a reply to this message
|
|