Composite numbers k with no single digit factors whose factors are all substrings of k: Difference between revisions
Content added Content deleted
(Add PARI/GP implementation) |
(→{{header|PARI/GP}}: beautify the PARI/GP code) |
||
Line 694: | Line 694: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<syntaxhighlight lang="PARI/GP"> |
<syntaxhighlight lang="PARI/GP"> |
||
/* Returns a substring of str starting at s with length n */ |
|||
ssubstr(str,s=1,n=0)={ |
ssubstr(str, s = 1, n = 0) = { |
||
my(vt=Vecsmall(str),ve,vr,vtn=#str,n1); |
my(vt = Vecsmall(str), ve, vr, vtn = #str, n1); |
||
⚫ | |||
if( |
if (vtn == 0, return("")); |
||
⚫ | |||
n1=vtn-s+1; if(n==0,n=n1); if(n>n1,n=n1); |
n1 = vtn - s + 1; if (n == 0, n = n1); if (n > n1, n = n1); |
||
ve=vector(n,z,z-1+s); vr=vecextract(vt,ve); return(Strchr(vr)); |
ve = vector(n, z, z - 1 + s); vr = vecextract(vt, ve); return(Strchr(vr)); |
||
} |
} |
||
/* Checks if subStr is a substring of mainStr */ |
|||
isSubstring(mainStr, subStr) = { |
isSubstring(mainStr, subStr) = { |
||
mainLen = #Vecsmall(mainStr); |
mainLen = #Vecsmall(mainStr); |
||
subLen = #Vecsmall(subStr); |
subLen = #Vecsmall(subStr); |
||
for (startPos=1, mainLen-subLen+1, |
for (startPos = 1, mainLen - subLen + 1, |
||
if (ssubstr(mainStr, startPos, subLen) == subStr, |
if (ssubstr(mainStr, startPos, subLen) == subStr, |
||
return(1); /* True: subStr found in mainStr */ |
|||
) |
) |
||
); |
); |
||
return(0); /* False: subStr not found */ |
|||
} |
} |
||
⚫ | |||
⚫ | |||
contains_its_prime_factors_all_over_9(n) = { |
contains_its_prime_factors_all_over_9(n) = { |
||
if(n < 10 || isprime(n), return(0)); |
if (n < 10 || isprime(n), return(0)); /* Skip if n < 10 or n is prime */ |
||
strn = Str(n); |
strn = Str(n); /* Convert n to string */ |
||
pfacs = factor(n)[,1]; |
pfacs = factor(n)[, 1]; /* Get unique prime factors of n */ |
||
for (i = 1, #pfacs, |
|||
if (pfacs[i] <= 9, return(0)); /* Skip factors ≤ 9 */ |
|||
for(i=1, #pfacs, |
|||
if(pfacs[i] |
if (!isSubstring(strn, Str(pfacs[i])), return(0)); /* Check if factor is a substring */ |
||
if(!isSubstring(strn, Str(pfacs[i])), return(0)); \\ Check if factor is not a substring |
|||
); |
); |
||
return(1); |
return(1); /* All checks passed */ |
||
} |
} |
||
/* Main loop to find and print numbers meeting the criteria */ |
|||
{ |
{ |
||
found = 0; |
found = 0; /* Counter for numbers found */ |
||
for(n=0, 30*10^6, |
for (n = 0, 30 * 10^6, /* Iterate from 0 to 30 million */ |
||
if(contains_its_prime_factors_all_over_9(n), |
if (contains_its_prime_factors_all_over_9(n), |
||
found += 1; |
found += 1; /* Increment counter if n meets criteria */ |
||
print1(n, " "); |
print1(n, " "); /* Print n followed by a space */ |
||
if(found % 10 == 0, print("")); |
if (found % 10 == 0, print("")); /* Newline every 10 numbers */ |
||
if(found == 20, break); |
if (found == 20, break); /* Stop after finding 20 numbers */ |
||
); |
); |
||
); |
); |