Skip to main content

Prime or Not...check and flag



Found this problem on one of the sites.
The idea is to identify a series of numbers as prime based on their ordinal and flag them accordingly based on the following conditions :

a) If prime, concatenate its ordinal in the array to the output string.
b) If not prime, concatenate zero.

Here is the algorithm in C#.

string statstring = String.Empty;
bool ipr = false;
double[] primetest = { 2, 65537, 2999999, 4999999, 98320414332827, 2 ^ 35 - 1, 34567875111, 12345678987654321 };

for (int i = 0; i < 8; i++)
{
ipr = IsPrimeNumber(primetest[i]);

if (ipr == true)
{
statstring += (i + 1).ToString();
}
else
{
statstring += "0";
}
}

MessageBox.Show(statstring);

static bool IsPrimeNumber(double num)
{

bool bPrime = true;
double factor = num / 2;
int i = 0;

for (i = 2; i <= Math.Sqrt(num); i++)
{
if ((num % i) == 0)
{
bPrime = false;
break;
}
}
return bPrime;
}

IsPrimeNumber is a function that determines if a given number is prime or not. Got this on the net, the only improvement being that I added break condition in the loop to exit if prime. (reduces the number of iterations).

Comments

Popular posts from this blog

Chess blogging - Chessbase is back with a bang !

In view of my keen interest in UI for applications and websites, I feel it necessary to promote an investigation of the new play-chess interface on www.chessbase.com . * It is minimalistic * Allows you to play as Guest * Allows you to choose from different time-controls * Allows you to test your skills vis-a-vis opponents from across the globe Have a look at this interface, that is impressive from a UI developers point-of-view, and very conducive for a chess player to test his wits !  Whoa !  😇😇😇

Tomas ♟️Bot pgn

I beat this bot with an ELO of 1200 in Fischer 960 Chess ♟️  [Event "Vs. Computer"] [Site "Chess.com"] [Date "2025-01-28"] [White "petrushka_googol"] [Black "Tomas"] [Result "1-0"] [WhiteElo "400"] [BlackElo "1200"] [TimeControl "-"] [SetUp "1"] [FEN "qrkbbnnr/pppppppp/8/8/8/8/PPPPPPPP/QRKBBNNR w HBhb - 0 1"] [Variant "Chess960"] [Termination "petrushka_googol won by checkmate"] 1. e3 d6 2. Bf3 Bd7 3. Ng3 c5 4. Nh3 Bxh3 5. gxh3 Ba5 6. c3 h5 7. b4 Bc7 8. bxc5 dxc5 9. Nxh5 Nf6 10. Nxg7 Rh7 11. Nf5 e6 12. Ne7+ Kd8 13. O-O-O a6 14. c4 Kxe7 15. Rg1 Rd8 16. d3 Rh6 17. Bg4 Nxg4 18. hxg4 Qb8 19. g5 Rh7 20. Qf6+ Ke8 21. h4 Rd6 22. f4 Rb6 23. Rh1 Ng6 24. h5 Bd8 25. Qc3 Nf8 26. g6 Rb1+ 27. Kd2 Rxd1+ 28. Kxd1 fxg6 29. Rg1 g5 30. Bg3 Be7 31. fxg5 Qd8 32. Be5 Rxh5 33. Bf6 Bxf6 34. gxf6 Rh1 35. Rxh1 Qb6 36. Qc1 Kd8 37. Rh8 e5 38. Rxf8+ Kc7 39. Rf7+ Kc8 40. Re7 Qxf6 41. Re8...

B Trees🌲🌳🌴 databases and CHESS☺

Binary trees are a specific version of decision trees that have two nodes,one parent and two children, and this concept is used in database programming with relational databases like SQL server for creating in clustered and non clustered indexes. Alpha beta pruning together with B tree searches makeup the basis of chess programming. I have played a program that reached its thought horizon and then had a bizarre response inspite of  having an advantage of one pawn♟️