| Bytes | Lang | Time | Link |
|---|---|---|---|
| 018 | AWK | 250730T141113Z | xrs |
| nan | 250209T093418Z | Rosario | |
| 060 | Befunge93 | 250208T174205Z | CrSb0001 |
| 022 | Desmos | 250209T022204Z | DesmosEn |
| 036 | Bash and coreutils | 250209T012931Z | David G. |
| 009 | Japt | 250210T174612Z | Shaggy |
| 042 | Befunge93 | 250209T234645Z | att |
| 014 | R | 250209T200203Z | pajonk |
| 004 | MathGolf | 250210T082305Z | Kevin Cr |
| 019 | JavaScript Node.js | 250210T003227Z | Lucenapo |
| 008 | Husk | 250209T174140Z | Glory2Uk |
| 007 | Jelly | 250208T173806Z | Fmbalbue |
| 013 | Charcoal | 250209T180448Z | Neil |
| 004 | Jelly | 250209T171418Z | Jonathan |
| 009 | Uiua 0.15.0dev.1 | 250208T204146Z | Tbw |
| 038 | Retina 0.8.2 | 250208T231520Z | Neil |
| 076 | Haskell | 250208T231117Z | matteo_c |
| 022 | Python 3 | 250208T062151Z | l4m2 |
AWK, 18 bytes
$0>0{$0=30^32%$1}1
Port of the solution going around. Prints 0 for truthy, and some other int for falsey.
$0>0 # catch negatives
{$0= # set output
30^32%$1} # magic sauce
1 # force output
APL(NARS), 15 chars
{⍵<1:0⋄(⍳5)⊇π⍵}
This seems ok for all the test cases... test:
f←{⍵<1:0⋄(⍳5)⊇π⍵}
{⍵,f⍵}¨¯1 0 1 2 7 ¯4294967296 4294967296 42050 60060 60
¯1 0 0 0 1 1 2 1 7 0 ¯4294967296 0 4294967296 1 42050 0 60060 0 60 1
Befunge-93, 60 bytes
Changelog:
2025-02-08: 224 bytes (-00) CrSb0001
2025-02-09: 156 bytes (-68) CrSb0001
2025-02-10: 105 bytes (-51) CrSb0001
2025-04-02: 080 bytes (-25) CrSb0001
2025-04-03: 060 bytes (-20) CrSb0001
Current program:
&>:2%|>:3%|>:5%|
^</2<^</3<^</5<
@,"T" _!@# -1<
>^ >^
This probably could made a bit more compact Probably as compact as it gets with my current method, but I don't want to mess around too much with it. I'm honestly happy with being able to fit it in a 5x21 5x16 4x16 playspace.
Some changes I made in the current program:
- Concerning the values 0 and 1:
- The 224 and 156 byte programs considered 0 and 1 to be special cases. This is because going through the loops with 0 would infinitely recurse through the check for divisibility by 2, and 1 is (trivially) an ugly number.
- The 105 byte program took note of this with checking if the logical NOT of the input was non-zero. If it was, then it outputted
F, else we were able to continue onto the loops. - However, what I decided to just do is let every single number move onto the loops. While this does mean we loop infinitely for input 0, IIRC I can just write this off as undefined behavior, as while
0%2, 0%3, 0%5all equal 0, dividing it by 2, 3 or 5 doesn't change the result. - Now, in the 60-byte solution, if it isn't an ugly number, the program ends without outputting anything (or if it's 0, then that's just undefined behavior). This is allowed as the output for truthy and falsy values are different.
Original 224 bytes solution
&v>:1-#v_" T",@, "T"$ <
@:^ >:2%| >:3%| >:5%|^<
^#^ ^ # ^ # ^ #
^>^ ^</2<<^</3<<^</5<<
^|< :$^ :$^ :$
^"^ >^^ >^^ >^
,F^ >>^ >>^ v_>^
^"^<<<<<<<< <<<<< <<<<<-
^<^ 1
This is actually what I used to test the test cases :D
You can try it online using this interpreter.
Basically the reason this works is because the pointer basically moves around on a torus (if I remember correctly) so we don't need to waste even more space on the playing field than needed.
Desmos, 22 bytes
-50 bytes thanks to Aiden Chow's port of l4m2's solution
-1 byte thanks to emanresu A remembering Desmos makes complete sense
f(n)=mod(30^3^2,n)+0^n
Returns 0 for truthy, any anything else for falsey. This works because mod(x,n) for our purposes returns 0 if at least one of the prime factors of x divides evenly into n. 30's prime factors are 2, 3, and 5, and raising it to the 32 only changes the multiplicity of these factors. So since our function checks divisibility of n by 30^32, it is essentially checking if n is divisible by 2, 3, or 5. Exponentiating 30 is necessary since if n > x, it follows that remainder(x/n) = x.
Bash and coreutils, 70 67 45 43 36 bytes
-3 by removing "-- ", changes the error internally.
-22 thanks to @l4m2 by appending 0 and only testing for largest factor 5
-2 more thanks to @l4m2 -- I didn't need the quoting I re-inserted
-7 thanks to @corvus_192. I chose == instead of =~ (both work) and will not claim for letting errors go to stderr.
F(){
[[ `factor $10 2>&1` == *5 ]]
}
The truth value is the normal function exit status.
Explanation
First I append "0" to the number. This guarantees the number isn't blank or --, and adds factors of 2 and 5 to a real number.
Then factor either outputs an error, or a factorization of the input. Taking the last word of that, it is either "integer" (from ... not a positive integer), "information." (from ... --help' for more information.), "0:", "1:", or the largest prime factor. I then return true for "2", "3", "5", or "1:". The pattern test works because the only prime that ends in 5 is 5.
Notes
If I didn't have to handle negatives (and other weird stuff), I could lose 5 bytes. If I didn't have to handle 1, another 3.
I did save two bytes by not quoting "$1", which means that F '' and F '13 2', and now F --, will not work reasonably, but then the problem stipulated integer input.
On the other hand, I can handle "junk", "--3" (false, it isn't an integer), and "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" (One Googol, true). Modern coreutils is nice. :-)
It should be noted that, since I do a complete factorization of the number, the time requirements for larger integers can get ridiculous. Generating some RSA keys, a 72-bit modulus (product of 2 primes) has no trouble, but at 128 bits it easily times out Try it online. On the other hand, factor apparently has primality tests, and can handle a 128 bit prime in under a second. For those who would like to try my tests: 267997323221996804572992601411539070927 and 330700570015040716567014989185436824427
Japt, 9 bytes
>T«Uk d¨7
>T«Uk d¨7 :Implicit input of integer U
>T :Greater than 0?
« :Logical AND with logical NOT of
Uk : Prime factors of U
d : Any
¨7 : Greater than or equal to 7?
Befunge-93, 42 bytes
&:|
FT>>1-!1g,@
2:<|%5:_3/1v%
2/^>5/1^%3:_
Try it online! or Try all testcases
→ &:| input
2:< v%
2/^ _
↑ divide out 2s
_3/1v ← divide out 3s
^%3:_
|%5:_
>5/1^ ← divide out 5s
↓ skip to here if input=0
FT>>1-!1g,@ output T if 1, else F
↑
MathGolf, 4 bytes
S▬\÷
Port of @l4m2's Python answer, but uses n instead of 32.
Outputs 1 if the number is ugly and 0 or an error if not.
Try it online (the largest test cases are omitted).
Explanation:
S # Push 30
▬ # Push 30 to the power the (implicit) input
\ # Swap so the input is at the top of the stack
÷ # Check if 30**input is divisible by the input
# (errors for -1, since 30**-1 is a float; errors for 0, due to division-by-0)
# (after which the result is output implicitly)
JavaScript (Node.js), 19 bytes
n=>n>0&&30n**n%n<1n
If you require a solution that runs in reasonable time:
JavaScript (Node.js), 21 bytes
n=>n>0&&30n**32n%n<1n
Husk, 8 bytes
=V>5p¹ε→
Outputs 1 if the number is ugly, 0 otherwise. Works for the numbers starting from -2, which satisfies the question requirements. See also this Jelly answer.
Commented:
ε→ -- treating special cases: -2, -1, 0
p¹ -- make a list of prime divisors
V>5 -- is any of them greater than 5?
= -- compare two conditions
Jelly, 7 bytes
ÆffƑ5R¤
Thanks to Glory2Ukraine for pointing out a bug, and Jonathan Allan for finding a solution to fix the bug.
Charcoal, 13 bytes
Nθ∧›θ⁰¬﹪X⁶⁰φθ
Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if X is ugly, nothing if not. Works for integers below 3¹⁰⁰¹. Explanation:
Nθ Input `X` as a number
θ Input `X`
› Is greater than
⁰ Literal integer `0`
∧ Logical And
⁶⁰ Literal integer `60`
X Raised to power
φ Predefined variable `1000`
﹪ Modulo
θ Input `X`
¬ Is zero
Implicitly print
11 bytes if the input is guaranteed to be formatted as an integer, such that e.g. 1 and 00 are invalid inputs:
∧›θ0¬﹪X⁶⁰φN
Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if X is ugly, nothing if not. Works for integers below 3¹⁰⁰¹. Explanation:
∧›θ0¬﹪X⁶⁰φN
θ Input `X` as a string
› Is greater than
0 Literal string `0`
∧ Logical And
⁶⁰ Literal integer `60`
X Raised to power
φ Predefined variable `1000`
﹪ Modulo
N Input `X` as a number
¬ Is zero
Implicitly print
For even larger values of X you could replace ⁶⁰φθ with ³⁰θθ but it would be impractically slow, so for fast code the best I could do was for 22 bytes:
NθW∧θΦ235¬﹪θIκ≧÷Πιθ⁼¹θ
Try it online!Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if X is ugly, nothing if not. Explanation:
Nθ
Input X.
W∧θΦ235¬﹪θIκFι
Repeat while X is nonzero but has at least one of 2, 3 or 5 as a factor...
≧÷Πιθ
... divide X once by the product of the found factors.
⁼¹θ
Check that X is now 1.
Jelly, 4 bytes
Port of l4m2's Python answer but using \$n\$ rather than \$32\$ since this is code-golf.
30*%
A monadic Link that accepts an integer and yields inverted: zero (falsey) if ugly or non-zero (truthy) if not.
How?
30*% - Link: integer, N
30 - thirty (i.e. 2*3*5)
* - raised to the power {N}
% - modulo {N} (1%0 -> inf, which is truthy as needed)
Uiua 0.15.0-dev.1, 9 bytes SBCS
≤5/↥⍣°/×∞
Returns 1 for ugly numbers and 0 otherwise.
Explanation
≤5/↥⍣°/×∞
⍣°/× # try to factor (literally un-(foldr by product))
⍣ ∞ # catch error, then push infinity if so
/↥ # maximum
≤5 # less than or equal to 5
Retina 0.8.2, 38 bytes
\d+
$*
+`^(111?|1{5})(\1)*$
1$#2$*
^1$
Try it online! Link includes viable test cases (Retina will run out of memory for very large inputs). Explanation:
\d+
$*
Convert the numeric part of the input to unary.
+`^(111?|1{5})(\1)*$
1$#2$*
Repeatedly divide by 2, 3 or 5.
^1$
Check whether the result is 1.
Haskell, 76 bytes
_#n|n*n<2=n*n
z@(x:y)#n|(d,0)<-divMod n x=z#d
(x:y)#n=y#n
_#n=0
f=([2,3,5]#)
Python 3, 22 bytes
lambda n:n>0==30**32%n
Technically **n work but would too slow so I use that n<=2^32