Function Repository Resource:

FindFirstNumberInstance

Source Notebook

Find the first number that matches a certain requirement

Contributed by: Sander Huisman

ResourceFunction["FindFirstNumberInstance"][f]

finds the first integer instance from the range 1 to ∞ for which f returns True

ResourceFunction["FindFirstNumberInstance"][f,max]

finds the first integer instance from the range 1 to max for which f returns True

ResourceFunction["FindFirstNumberInstance"][f,{min, max}]

finds the first integer instance from the range min to max for which f returns True

ResourceFunction["FindFirstNumberInstance"][f,{min,max,step}]

finds the first instance from the range min to max in steps of step for which f returns True

ResourceFunction["FindFirstNumberInstance"][f,spec, n]

finds the first n instances for which f returns True

Details

The third argument n can be set to ∞, and can return all the cases for the case that spec is a finite range.
Unlike in SelectFirst or FirstCase, no upper bound needs to be given.

Examples

Basic Examples (2) 

Find the first prime starting from 1:

In[1]:=
ResourceFunction["FindFirstNumberInstance"][PrimeQ]
Out[1]=

Find the first 10 composite numbers:

In[2]:=
ResourceFunction[
 "FindFirstNumberInstance"][CompositeQ, {1, \[Infinity]}, 10]
Out[2]=

Scope (5) 

Find the first 15 square numbers that are a multiple of 7:

In[3]:=
ResourceFunction["FindFirstNumberInstance"][
 IntegerQ@*Sqrt, {0, \[Infinity], 7}, 15]
Out[3]=

Find the first 15 palindromic primes:

In[4]:=
ResourceFunction["FindFirstNumberInstance"][
 PrimeQ[#] && PalindromeQ[#] &, \[Infinity], 15]
Out[4]=

Find the first 12 palindromes that are a multiple of 3:

In[5]:=
ResourceFunction[
 "FindFirstNumberInstance"][PalindromeQ, {0, \[Infinity], 3}, 12]
Out[5]=

Use an iterator with Real step size:

In[6]:=
ResourceFunction["FindFirstNumberInstance"][
 LessThan[5], {0, 10, 0.1}, 12]
Out[6]=

The iterator can not only be numbers, it can also be a symbolic expression:

In[7]:=
ResourceFunction["FindFirstNumberInstance"][
 Factor[# + x^2 + 4] === # + x^2 + 4 &, {x, 10 x, x}, 5]
Out[7]=

Possible Issues (2) 

FindFirstInstance will return less instances if no more instances exist:

In[8]:=
ResourceFunction["FindFirstNumberInstance"][PrimeQ, {0, 10}, 12]
Out[8]=

For the case that there is no third argument and there is no match, FindFirstNumberInstance returns Missing:

In[9]:=
ResourceFunction["FindFirstNumberInstance"][# == # + 1 &, {0, 100}]
Out[9]=

An empty list is returned if there is a third argument:

In[10]:=
ResourceFunction[
 "FindFirstNumberInstance"][# == # + 1 &, {0, 100}, \[Infinity]]
Out[10]=

Neat Examples (2) 

Find the first 12 palindromic composites that are a multiple of 7:

In[11]:=
ResourceFunction["FindFirstNumberInstance"][
 PalindromeQ[#] && CompositeQ[#] &, {0, \[Infinity], 7}, 12]
Out[11]=

Find the first 20 emirp primes:

In[12]:=
ResourceFunction["FindFirstNumberInstance"][
 PrimeQ[#] && PrimeQ[IntegerReverse[#]] && ! PalindromeQ[#] &, \[Infinity], 20]
Out[12]=

Publisher

SHuisman

Version History

  • 1.0.1 – 04 October 2022
  • 1.0.0 – 23 September 2022

Related Resources

License Information