# [Solved] A challenge- from physics with love

0

I'll be honest... I know NOTHING about computer science. But we both agree on one thing. We all like maths. So here is a little challenge- from physicists with love.

So,  glitch prime is any prime that is made up of primarily 1 unit, with a single digit in the middle made up of another unit number.

A famous example is this 506-digit monstrosity:

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999989999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Now, a cyclops prime is a glitch prime in binary. A much more petite example is 5, which in binary is 101.

Now, as a starting problem, I want you to find the smallest cyclops prime, greater than 50 digits long. I kinda get java, but any language is good, as long as you get an answer of course. Happy coding (also, in case this isn't challenging enough, wait until NEXT week's challenge 😉 )

"Either this wallpaper goes, or I go"- Oscar Wilde's last words

0

Great challenge Saeed! For my first attempt I went with Pascal as it is quite easy to understand if you're new to coding. Though, it doesn't handle large numbers very well... which was the fatal flaw.

```program Project1;

{\$mode objfpc}{\$H+}

uses sysUtils, strUtils;

var
binaryNum: string;
i, j: integer;

function BinToDec(bin: string): longint;
var
i, j : integer;
begin
j:= 1;
BinToDec:= 0;
for i:= length(bin) downto 1 do
begin
if bin[i]='1' then
BinToDec+= j;
j*= 2;
end;
end;

function CheckPrime(n: longint): boolean;
var
i: integer;
begin
// Checks if n/i has a remainder of 0 where
// 1 < i < n
i:= 1;
repeat
inc(i);
until (i=n-1) or (n mod i = 0);
if i=n-1 then
CheckPrime:= True
else
CheckPrime:= False;
end;

begin
// Initialising binaryNum
binaryNum:= '101';
i:= 2;
while True do
begin
// Output the number if prime
if CheckPrime(BinToDec(binaryNum)) then
writeln(binaryNum,' Position of 0: ',i);
// Increase length by 2
binaryNum:= binaryNum+'XX';
// Format the number
for j:= 1 to length(binaryNum) do
if j=(length(binaryNum) div 2)+1 then
binaryNum[j]:= '0'
else
binaryNum[j]:= '1';
inc(i);
end;
end.```

The only output I had that was genuine was:

`101 Position of 0: 2`

The rest was hogwash, so I will be translating it to Java! Will update.

"Computer science is no more about computers than astronomy is about telescopes."
~ Edsger W. Dijkstra

1

Hmm... I seem to be having an issue. If anyone knows how to use the StringBuilder data-type in Java, if you could tell me where this is going wrong, that would be fantastic.

```public class PrimeHunt {
public static int BinToDec(StringBuilder n) {  int j = 1;  int r = 0;  for (int i=0; i<n.length(); i++) {   if (n.charAt(i)=='1') {    r+= j;   }  j*= 2;  }  return r; }  public static boolean CheckPrime(int n) {   for (int i=2; i<n; i++) {    if (n % i == 0) {     return false;    }   }  return true; }  public static void main(String[] args) {  // TODO Auto-generated method stub  StringBuilder binaryNum = new StringBuilder("101");  while (1==1){   if (CheckPrime(BinToDec(binaryNum))) {    System.out.println(binaryNum);    System.out.println(BinToDec(binaryNum));   }   binaryNum.append("XX");   for (int j=0; j<binaryNum.length(); j++) {    if (j==(binaryNum.length()/2)+1){     binaryNum.setCharAt(j, '1');    } else {     binaryNum.setCharAt(j, '0');    }   }  } }}```

May have to translate to another language... but that's the fun!

"Computer science is no more about computers than astronomy is about telescopes."
~ Edsger W. Dijkstra

Share:

Working