0
package test;
import java.util.Random;
public class Test {

public static void main(String[] args) {

Random r = new Random();
int low = 15000;
int high = 25000;
int result = r.nextInt(high-low) +low;
System.out.println(result);

String fibo = "(?x) .? | ( \\2?+ (\\1|^.) )* ..";
for (int n = 0; n < 25000; n++) {
String s = new String(new char[n]);
if (s.matches(fibo)) {
System.out.printf("%s ", n);
}   
 }  
}
 }

Output:

18363 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711

With this code i get this result,but actually should do this :

if the number produced is not fibonacci:

Output: 23705 This number is not in the fibonacci series.

if the number produced is fibonacci:

Output: 24610 This number is in the fibonacci series.

How can we check if a randomly generated number between two numbers is fibonacci?

  • 4
    https://www.geeksforgeeks.org/check-number-fibonacci-number/ – cegredev May 14 '20 at 15:07
  • 2
    That’s a pretty gnarly-looking regex. Can you talk us through how you came up with it and what it’s supposed to do? – templatetypedef May 14 '20 at 15:13
  • `24610` isn't [a fibonacci number](http://oeis.org/A000045).. :/ – Kevin Cruijssen May 14 '20 at 15:23
  • @templatetypedef,It is only necessary to check whether the number produced is fibonacci and print it on the screen. – micheal May 14 '20 at 15:23
  • Can't you just remove the `for`-loop and just use `String s = new String(new char[result]); if (s.matches(fibo)) { System.out.printf("%s This number is in the fibonacci series.", result); } else{ System.out.printf("%s This number is not in the fibonacci series.", result); }`? [Like this?](https://ideone.com/JwIvcv) – Kevin Cruijssen May 14 '20 at 15:25
  • 1
    You still haven't explained how the regex works. Look at what Schred posted. – David Conrad May 14 '20 at 15:27
  • Sorry, to clarify, you’ve told us *what the regex is supposed to do* rather than *how you intend for it to work*. Can you walk us through the design of this regular expression, explaining how the capture groups and the like work? – templatetypedef May 14 '20 at 15:32
  • @Kevin Cruijssen Thanks a lot for your help. – micheal May 14 '20 at 15:57
  • 1
    @templatetypedef I'm not OP, but [here an explanation of the regex](https://pastebin.com/TwJetpCi) in case you are curious how it works. It's obviously too long to put in a comment, hence this pastebin. ;) – Kevin Cruijssen May 14 '20 at 16:05

0 Answers0