Am I not good enough for you?
$begingroup$
Background:
The current Perfect Numbers challenge is rather flawed and complicated, since it asks you to output in a complex format involving the factors of the number. This is a purely decision-problem repost of the challenge.
Challenge
Given a positive integer through any standard input format, distinguish between whether it is perfect or not.
A perfect number is a number that is equal to the sum of all its proper divisors (its positive divisors less than itself). For example, $6$ is a perfect number, since its divisors are $1,2,3$, which sum up to $6$, while $12$ is not a perfect number since its divisors ( $1,2,3,4,6$ ) sum up to $16$, not $12$.
Test Cases:
Imperfect:
1,12,13,18,20,1000,33550335
Perfect:
6,28,496,8128,33550336,8589869056
Rules
- Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
- Output can be two distinct and consistent values through any allowed output format. If it isn't immediately obvious what represents Perfect/Imperfect, please make sure to specify in your answer.
code-golf number decision-problem number-theory factoring
$endgroup$
|
show 5 more comments
$begingroup$
Background:
The current Perfect Numbers challenge is rather flawed and complicated, since it asks you to output in a complex format involving the factors of the number. This is a purely decision-problem repost of the challenge.
Challenge
Given a positive integer through any standard input format, distinguish between whether it is perfect or not.
A perfect number is a number that is equal to the sum of all its proper divisors (its positive divisors less than itself). For example, $6$ is a perfect number, since its divisors are $1,2,3$, which sum up to $6$, while $12$ is not a perfect number since its divisors ( $1,2,3,4,6$ ) sum up to $16$, not $12$.
Test Cases:
Imperfect:
1,12,13,18,20,1000,33550335
Perfect:
6,28,496,8128,33550336,8589869056
Rules
- Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
- Output can be two distinct and consistent values through any allowed output format. If it isn't immediately obvious what represents Perfect/Imperfect, please make sure to specify in your answer.
code-golf number decision-problem number-theory factoring
$endgroup$
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
2
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than1
would be perfect, since every number is divisible by1
and itself. The sum of proper divisors of1
is0
$endgroup$
– Jo King
Mar 12 at 7:40
3
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
1
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
3
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15
|
show 5 more comments
$begingroup$
Background:
The current Perfect Numbers challenge is rather flawed and complicated, since it asks you to output in a complex format involving the factors of the number. This is a purely decision-problem repost of the challenge.
Challenge
Given a positive integer through any standard input format, distinguish between whether it is perfect or not.
A perfect number is a number that is equal to the sum of all its proper divisors (its positive divisors less than itself). For example, $6$ is a perfect number, since its divisors are $1,2,3$, which sum up to $6$, while $12$ is not a perfect number since its divisors ( $1,2,3,4,6$ ) sum up to $16$, not $12$.
Test Cases:
Imperfect:
1,12,13,18,20,1000,33550335
Perfect:
6,28,496,8128,33550336,8589869056
Rules
- Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
- Output can be two distinct and consistent values through any allowed output format. If it isn't immediately obvious what represents Perfect/Imperfect, please make sure to specify in your answer.
code-golf number decision-problem number-theory factoring
$endgroup$
Background:
The current Perfect Numbers challenge is rather flawed and complicated, since it asks you to output in a complex format involving the factors of the number. This is a purely decision-problem repost of the challenge.
Challenge
Given a positive integer through any standard input format, distinguish between whether it is perfect or not.
A perfect number is a number that is equal to the sum of all its proper divisors (its positive divisors less than itself). For example, $6$ is a perfect number, since its divisors are $1,2,3$, which sum up to $6$, while $12$ is not a perfect number since its divisors ( $1,2,3,4,6$ ) sum up to $16$, not $12$.
Test Cases:
Imperfect:
1,12,13,18,20,1000,33550335
Perfect:
6,28,496,8128,33550336,8589869056
Rules
- Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
- Output can be two distinct and consistent values through any allowed output format. If it isn't immediately obvious what represents Perfect/Imperfect, please make sure to specify in your answer.
code-golf number decision-problem number-theory factoring
code-golf number decision-problem number-theory factoring
edited Mar 12 at 4:08
Jo King
asked Mar 12 at 2:41
Jo KingJo King
26.8k365132
26.8k365132
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
2
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than1
would be perfect, since every number is divisible by1
and itself. The sum of proper divisors of1
is0
$endgroup$
– Jo King
Mar 12 at 7:40
3
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
1
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
3
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15
|
show 5 more comments
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
2
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than1
would be perfect, since every number is divisible by1
and itself. The sum of proper divisors of1
is0
$endgroup$
– Jo King
Mar 12 at 7:40
3
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
1
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
3
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
2
2
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than
1
would be perfect, since every number is divisible by 1
and itself. The sum of proper divisors of 1
is 0
$endgroup$
– Jo King
Mar 12 at 7:40
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than
1
would be perfect, since every number is divisible by 1
and itself. The sum of proper divisors of 1
is 0
$endgroup$
– Jo King
Mar 12 at 7:40
3
3
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
1
1
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
3
3
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15
|
show 5 more comments
45 Answers
45
active
oldest
votes
1 2
next
$begingroup$
Brachylog, 4 bytes
fk+?
Try it online!
The predicate succeeds for perfect inputs and fails for imperfect inputs, printing true.
or false.
if run as a complete program (except on the last test case which takes more than a minute on TIO).
The input's
f factors
k without the last element
+ sum to
? the input.
$endgroup$
1
$begingroup$
I like how the code saysfk
:x
$endgroup$
– Ismael Miguel
Mar 13 at 1:38
add a comment |
$begingroup$
Neim, 3 bytes
𝐕𝐬𝔼
Try it online!
(I don't actually know how to run all of the test cases at once, since I started learning Neim about fifteen minutes ago, but I did check them individually.)
Prints 0 for imperfect, 1 for perfect.
𝐕 Pop an int from the stack and push its proper divisors,
implicitly reading the int from a line of input as the otherwise absent top of the stack.
𝐬 Pop a list from the stack and push the sum of the values it contains.
𝔼 Pop two ints from the stack and push 1 if they are equal, 0 if they are not;
implicitly reading the same line of input that was already read as the second int, I guess?
Implicitly print the contents of the stack, or something like that.
$endgroup$
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is𝔼
1 byte? Does Neim use only 128 such non-standart characters?
$endgroup$
– kajacx
Mar 13 at 13:39
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
add a comment |
$begingroup$
R, 33 29 bytes
!2*(n=scan())-(x=1:n)%*%!n%%x
Try it online!
Returns TRUE
for perfect numbers and FALSE
for imperfect ones.
$endgroup$
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped0
(perfect) toFALSE
and nonzero toTRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast fromnumeric
tological
, often in conjunction withwhich
or[
.
$endgroup$
– Giuseppe
Mar 12 at 3:37
add a comment |
$begingroup$
Jelly, 3 bytes
Æṣ=
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt -!
, 4 bytes
¥â¬x
-----------------
Implicit Input U
¥ Equal to
x Sum of
â Factors of U
¬ Without itself
For some reason ¦
doesnt work on tio so I need to use the -!
flag and ¥
instead
Try it online!
$endgroup$
$begingroup$
That's not a TIO issue;U
doesn't get auto-inserted before!
.
$endgroup$
– Shaggy
Mar 12 at 9:31
add a comment |
$begingroup$
Python 3, 46 bytes
lambda x:sum(i for i in range(1,x)if x%i<1)==x
Try it online!
Brute force, sums the factors and checks for equality.
$endgroup$
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.
$endgroup$
– nedla2004
Mar 12 at 13:13
add a comment |
$begingroup$
Python, 45 bytes
lambda n:sum(d*(n%d<1)for d in range(1,n))==n
True
for perfect; False
for others (switch this with ==
-> !=
)
Try it online!
44 42 41 bytes (-2 thanks to ovs) if we may output using "truthy vs falsey":
f=lambda n,i=1:i/n or-~f(n,i+1)-(n%i<1)*i
(falsey (0
)) for perfect; truthy (a non-zero integer) otherwise
$endgroup$
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
add a comment |
$begingroup$
Octave, 25 bytes
@(n)~mod(n,t=1:n)*t'==2*n
Try it online!
Explanation
@(n)~mod(n,t=1:n)*t'==2*n
@(n) % Define anonymous function with input n
1:n % Row vector [1,2,...,n]
t= % Store in variable t
mod(n, ) % n modulo [1,2,...,n], element-wise. Gives 0 for divisors
~ % Logical negate. Gives 1 for divisors
t' % t transposed. Gives column vector [1;2;...;n]
* % Matrix multiply
2*n % Input times 2
== % Equal? This is the output value
$endgroup$
add a comment |
$begingroup$
JavaScript, 38 bytes
n=>eval("for(i=s=n;i--;)n%i||!(s-=i)")
Try it online!
(Last testcase timeout on TIO.)
$endgroup$
$begingroup$
@Arnauld Just forgot to remove thef=
after converting from a recursive function.
$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 46 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)^n*2
Returns 0 if perfect, otherwise returns a positive number. I don't know if outputting different types of integers are allowed in place of two distinct truthy and falsy values, and couldn't find any discussion on meta about it. If this is invalid, I will remove it.
Try it online!
C# (Visual C# Interactive Compiler), 49 47 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)==n*2
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 33 bytes
->n{(1...n).sum{|i|n%i<1?i:0}==n}
Try it online!
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 30 23 bytes
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1
Horribly inefficient, but it works.
Reducing the bytecount sped up the program by a lot.
Input is in Ans
.
Output is in Ans
and is automatically printed out when the program completes.
Explanation:
(TI-BASIC doesn't have comments, so just assume that ;
makes a comment)
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans ;Full program
2Ans ;double the input
seq( ;generate a list
X, ;using the variable X,
1, ;starting at 1,
Ans ;and ending at the input
;with an implied increment of 1
Ans/X ;from the input divided by X
not( ), ;multiplied by the negated result of
remainder(Ans,X) ;the input modulo X
;(result: 0 or 1)
sum( ;sum up the elements in the list
= ;equal?
Example:
6
6
prgmCDGF2
1
7
7
prgmCDGF2
0
Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2
, (5 bytes) and an extra 8 bytes used for storing the program:
36 - 5 - 8 = 23 bytes
$endgroup$
add a comment |
$begingroup$
Java (JDK), 54 bytes
n->{int s=0,d=0;for(;++d<n;)s+=n%d<1?d:0;return s==n;}
Try it online!
Though for a strict number by number matching, the following will return the same values, but is only 40 bytes.
n->n==6|n==28|n==496|n==8128|n==33550336
Try it online!
$endgroup$
$begingroup$
The rules sayYour program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Javaint
at all, but rather aBigInteger
? Because Java hasBigIntegers
, but it won't ever have anint
that's more than 31 bits as signed, which can't hold any other value than those represented here...
$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if theint
type was unbounded
$endgroup$
– Jo King
Mar 13 at 21:16
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
add a comment |
$begingroup$
x86 Assembly, 45 43 Bytes.
6A 00 31 C9 31 D2 41 39 C1 7D 0B 50 F7 F9 58 85
D2 75 F1 51 EB EE 31 D2 59 01 CA 85 C9 75 F9 39
D0 75 05 31 C0 40 EB 02 31 C0 C3
Explaination (Intel Syntax):
PUSH $0 ; Terminator for later
XOR ECX, ECX ; Clear ECX
.factor:
XOR EDX, EDX ; Clear EDX
INC ECX
CMP ECX, EAX ; divisor >= input number?
JGE .factordone ; if so, exit loop.
PUSH EAX ; backup EAX
IDIV ECX ; divide EDX:EAX by ECX, store result in EAX and remainder in EDX
POP EAX ; restore EAX
TEST EDX, EDX ; remainder == 0?
JNZ .factor ; if not, jump back to loop start
PUSH ECX ; push factor
JMP .factor ; jump back to loop start
.factordone:
XOR EDX, EDX ; clear EDX
.sum:
POP ECX ; pop divisor
ADD EDX, ECX ; sum into EDX
TEST ECX, ECX ; divisor == 0?
JNZ .sum ; if not, loop.
CMP EAX, EDX ; input number == sum?
JNE .noteq ; if not, skip to .noteq
XOR EAX, EAX ; clear EAX
INC EAX ; increment EAX (sets to 1)
JMP .return ; skip to .return
.noteq:
XOR EAX, EAX ; clear EAX
.return:
RETN
Input should be provided in EAX
.
Function sets EAX
to 1
for perfect and to 0
for imperfect.
EDIT: Reduced Byte-Count by two by replacing MOV EAX, $1
with XOR EAX, EAX
and INC EAX
$endgroup$
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
add a comment |
$begingroup$
CJam, 17 bytes
ri_,(;{1$%!},:+=
Try it online!
$endgroup$
add a comment |
$begingroup$
Javascript, 62
n=>n==[...Array(n).keys()].filter(a=>n%a<1).reduce((a,b)=>a+b)
Explanation (although it's pretty simple)
n=> //return function that takes n
n== //and returns if n is equal to
[...Array(n).keys()] //an array [0..(n-1)]...
.filter(a=>n%a<1) //where all of the elements that are not divisors of n are taken out...
.reduce((a,b)=>a+b) //summed up
Thanks to Jo King for the improvement!
$endgroup$
add a comment |
$begingroup$
05AB1E, 4 bytes
ѨOQ
Try it online!
Explanation
O # the sum
Ñ # of the divisors of the input
¨ # with the last one removed
Q # equals the input
$endgroup$
add a comment |
$begingroup$
Powershell, 46 bytes 43 bytes
param($i)1..$i|%{$o+=$_*!($i%$_)};$o-eq2*$i
Try it Online!
Edit:
-3 bytes thanks to @AdmBorkBork
$endgroup$
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against2*$i
to eliminate the subtract-one parens.
$endgroup$
– AdmBorkBork
Mar 12 at 12:42
add a comment |
$begingroup$
C (gcc), 41 bytes
f(n,i,s){for(i=s=n;--i;s-=n%i?0:i);n=!s;}
Try it online!
1: 0
12: 0
13: 0
18: 0
20: 0
1000: 0
33550335: 0
6: 1
28: 1
496: 1
8128: 1
33550336: 1
-65536: 0 <---- Unable to represent final test case with four bytes, fails
Let me know if that failure for the final case is an issue.
$endgroup$
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
$begingroup$
@Neil Better yet, it can be fixed withn=!s;
instead ofreturn!s;
to save 5 bytes.
$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I dids=s
which more than likely got optimized out.
$endgroup$
– Marcos
Mar 13 at 0:26
add a comment |
$begingroup$
Smalltalk, 34 bytes
((1to:n-1)select:[:i|n\i=0])sum=n
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 45 bytes
: f 0 over 1 ?do over i mod 0= i * - loop = ;
Try it online!
Explanation
Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum equals n
Code Explanation
: f start word definition
0 over 1 create a value to hold the sum and setup the bounds of the loop
?do start a counted loop from 1 to n. (?do skips if start = end)
over copy n to the top of the stack
i mod 0= check if i divides n perfectly
i * - if so, use the fact that -1 = true in forth to add i to the sum
loop end the counted loop
= check if the sum and n are equal
; end the word definition
$endgroup$
add a comment |
$begingroup$
Pyth, 9 13 bytes
qsf!%QTSt
Try it online!
Thank you to the commentors for the golf help
Finds all the factors of the input, sums them, and compares that to the original input.
$endgroup$
$begingroup$
A few golfs for you -q0
can be replaced with!
, andSQ
produces the range[1-Q]
, so the range[1-Q)
can be generated usingStQ
. As theQ
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes -qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
add a comment |
$begingroup$
Labyrinth, 80 bytes
?::`}:("(!@
perfect:
{:{:;%"}
+puts; "
}zero: "
}else{(:
"negI" _~
""""""{{{"!@
The Latin characters perfect puts zero else neg I
are actually just comments*.
i.e. if the input is perfect a 0
is printed, otherwise -1
is.
Try it online!
* so this or this work too...
?::`}:("(!@ ?::`}:("(!@
: BEWARE :
{:{:;%"} {:{:;%"}
+ ; " +LAIR; "
} : " } OF : "
} {(: }MINO{(:
" " _~ "TAUR" _~
""""""{{{"!@ """"""{{{"!@
How?
Takes as an input a positive integer n
and places an accumulator variable of -n
onto the auxiliary stack, then performs a divisibility test for each integer from n-1
down to, and including, 1
, adding any which do divide n
to the accumulator. Once this is complete if the accumulator variable is non-zero a -1
is output, otherwise a 0
is.
The ?::`}:(
is only executed once, at the beginning of execution:
?::`}:( Main,Aux
? - take an integer from STDIN and place it onto Main [[n],]
: - duplicate top of Main [[n,n],]
: - duplicate top of Main [[n,n,n],]
` - negate top of Main [[n,n,-n],]
} - place top of Main onto Aux [[n,n],[-n]]
: - duplicate top of Main [[n,n,n],[-n]]
( - decrement top of Main [[n,n,n-1],[-n]]
The next instruction, "
, is a no-op, but we have three neighbouring instructions so we branch according to the value at the top of Main, zero takes us forward, while non-zero takes us right.
If the input was 1
we go forward because the top of Main is zero:
(!@ Main,Aux
( - decrement top of Main [[1,1,-1],[-1]]
! - print top of Main, a -1
@ - exit the labyrinth
But if the input was greater than 1
we turn right because the top of Main is non-zero:
:} Main,Aux
: - duplicate top of Main [[n,n,n-1,n-1],[-n]]
} - place top of Main onto Aux [[n,n,n-1],[-n,n-1]]
At this point we have a three-neighbour branch, but we know n-1
is non-zero, so we turn right...
"% Main,Aux
" - no-op [[n,n,n-1],[-n,n-1]]
% - place modulo result onto Main [[n,n%(n-1)],[-n,n-1]]
- ...i.e we've got our first divisibility indicator n%(n-1), an
- accumulator, a=-n, and our potential divisor p=n-1:
- [[n,n%(n-1)],[a,p]]
We are now at another three-neighbour branch at %
.
If the result of %
was non-zero we go left to decrement our potential divisor, p=p-1
, and leave the accumulator, a
, as it is:
;:{(:""}" Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
- three-neighbour branch but n-1 is non-zero so we turn left
( - decrement top of Main [[n,n,p-1],[a]]
: - duplicate top of Main [[n,n,p-1,p-1],[a]]
"" - no-ops [[n,n,p-1,p-1],[a]]
} - place top of Main onto Aux [[n,n,p-1],[a,p-1]]
" - no-op [[n,n,p-1],[a,p-1]]
% - place modulo result onto Main [[n,n%(p-1)],[a,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if the result of %
was zero (for the first pass only when n=2
) we go straight on to BOTH add the divisor to our accumulator, a=a+p
, AND decrement our potential divisor, p=p-1
:
;:{:{+}}""""""""{(:""} Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
: - duplicate top of Main [[n,n,p,p],[a]]
{ - place top of Aux onto Main [[n,n,p,p,a],]
+ - perform addition [[n,n,p,a+p],]
} - place top of Main onto Aux [[n,n,p],[a+p]]
} - place top of Main onto Aux [[n,n],[a+p,p]]
""""""" - no-ops [[n,n],[a+p,p]]
- a branch, but n is non-zero so we turn left
" - no-op [[n,n],[a+p,p]]
{ - place top of Aux onto Main [[n,n,p],[a+p]]
- we branch, but p is non-zero so we turn right
( - decrement top of Main [[n,n,p-1],[a+p]]
: - duplicate top of Main [[n,n,p-1,p-1],[a+p]]
"" - no-ops [[n,n,p-1,p-1],[a+p]]
} - place top of Main onto Aux [[n,n,p-1],[a+p,p-1]]
At this point if p-1
is still non-zero we turn left:
"% Main,Aux
" - no-op [[n,n,p-1],[a+p,p-1]]
% - modulo [[n,n%(p-1)],[a+p,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if p-1
hit zero we go straight up to the :
on the second line of the labyrinth (you've seen all the instructions before, so I'm leaving their descriptions out and just giving their effect):
:":}"":({):""}"%;:{:{+}}"""""""{{{ Main,Aux
: - [[n,n,0,0],[a,0]]
" - [[n,n,0,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
: - [[n,n,0,0,0],[a,0]]
} - [[n,n,0,0],[a,0,0]]
- top of Main is zero so we go straight
"" - [[n,n,0,0],[a,0,0]]
: - [[n,n,0,0,0],[a,0,0]]
( - [[n,n,0,0,-1],[a,0,0]]
{ - [[n,n,0,0,-1,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
( - [[n,n,0,0,-1,-1],[a,0]]
: - [[n,n,0,0,-1,-1,-1],[a,0]]
"" - [[n,n,0,0,-1,-1,-1],[a,0]]
} - [[n,n,0,0,-1,-1],[a,0,-1]]
- top of Main is non-zero so we turn left
" - [[n,n,0,0,-1,-1],[a,0,-1]]
% - (-1)%(-1)=0 [[n,n,0,0,0],[a,0,-1]]
; - [[n,n,0,0],[a,0,-1]]
: - [[n,n,0,0,0],[a,0,-1]]
{ - [[n,n,0,0,0,-1],[a,0]]
: - [[n,n,0,0,0,-1,-1],[a,0]]
{ - [[n,n,0,0,0,-1,-1,0],[a]]
+ - [[n,n,0,0,0,-1,-1],[a]]
} - [[n,n,0,0,0,-1],[a,-1]]
} - [[n,n,0,0,0],[a,-1,-1]]
""""""" - [[n,n,0,0,0],[a,-1,-1]]
- top of Main is zero so we go straight
{ - [[n,n,0,0,0,-1],[a,-1]]
{ - [[n,n,0,0,0,-1,-1],[a]]
{ - [[n,n,0,0,0,-1,-1,a],]
Now this {
has three neighbouring instructions, so...
...if a
is zero, which it will be for perfect n
, then we go straight:
"!@ Main,Aux
" - [[n,n,0,0,0,-1,-1,a],]
- top of Main is a, which is zero, so we go straight
! - print top of Main, which is a, which is a 0
@ - exit the labyrinth
...if a
is non-zero, which it will be for non-perfect n
, then we turn left:
_~"!@ Main,Aux
_ - place a zero onto Main [[n,n,0,0,0,-1,-1,a,0],]
~ - bitwise NOT top of Main (=-1-x) [[n,n,0,0,0,-1,-1,a,-1],]
" - [[n,n,0,0,0,-1,-1,a,-1],]
- top of Main is NEGATIVE so we turn left
! - print top of Main, which is -1
@ - exit the labyrinth
$endgroup$
add a comment |
$begingroup$
Batch, 81 bytes
@set s=-%1
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@if %s%==%1 echo 1
Takes n
as a command-line parameter and outputs 1
if it is a perfect number. Brute force method, starts the sum at -n
so that it can include n
itself in the loop.
$endgroup$
add a comment |
$begingroup$
Charcoal, 13 bytes
Nθ⁼θΣΦθ∧ι¬﹪θι
Try it online! Link is to verbose version of code. Outputs -
for perfect numbers. Uses brute force. Explanation:
Nθ Numeric input
Φθ Filter on implicit range
ι Current value (is non-zero)
∧ Logical And
θ Input value
﹪ Modulo
ι Current value
¬ Is zero
Σ Sum of matching values
⁼ Equals
θ Input value
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 14 bytes
PerfectNumberQ
Try it online!
$endgroup$
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
add a comment |
$begingroup$
Pyth, 8 bytes
qs{*MPyP
Try it online here.
qs{*MPyPQQ Implicit: Q=eval(input())
Trailing QQ inferred
PQ Prime factors of Q
y Powerset
P Remove last element - this will always be the full prime factorisation
*M Take product of each
{ Deduplicate
s Sum
q Q Is the above equal to Q? Implicit print
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 44 bytes
.+
$*
M!&`(.+)$(?<=^1+)
+`^1(1*¶+)1
$1
^¶+$
Try it online! Uses brute force, so link only includes the faster test cases. Explanation:
.+
$*
Convert to unary.
M!&`(.+)$(?<=^1+)
Match all factors of the input. This uses overlapping mode, which in Retina 0.8.2 requires all of the matches to start at different positions, so the matches are actually returned in descending order, starting with the original input.
+`^1(1*¶+)1
$1
Subtract the proper factors from the input.
^¶+$
Test whether the result is zero.
$endgroup$
add a comment |
$begingroup$
Java 8, 66 bytes
Someone has to use the stream API at some point, even if there's a shorter way to do it
n->java.util.stream.IntStream.range(1,n).filter(i->n%i<1).sum()==n
Try it online!
$endgroup$
add a comment |
$begingroup$
cQuents, 8 bytes
?#N=UzN
Try it online!
Explanation
? Mode query: return whether or not input is in sequence
# Conditional: iterate N, add N to sequence if condition is true
N= Condition: N ==
U ) sum( )
z ) proper_divisors( )
N N
)) implicit
$endgroup$
add a comment |
1 2
next
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181350%2fam-i-not-good-enough-for-you%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
45 Answers
45
active
oldest
votes
45 Answers
45
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
$begingroup$
Brachylog, 4 bytes
fk+?
Try it online!
The predicate succeeds for perfect inputs and fails for imperfect inputs, printing true.
or false.
if run as a complete program (except on the last test case which takes more than a minute on TIO).
The input's
f factors
k without the last element
+ sum to
? the input.
$endgroup$
1
$begingroup$
I like how the code saysfk
:x
$endgroup$
– Ismael Miguel
Mar 13 at 1:38
add a comment |
$begingroup$
Brachylog, 4 bytes
fk+?
Try it online!
The predicate succeeds for perfect inputs and fails for imperfect inputs, printing true.
or false.
if run as a complete program (except on the last test case which takes more than a minute on TIO).
The input's
f factors
k without the last element
+ sum to
? the input.
$endgroup$
1
$begingroup$
I like how the code saysfk
:x
$endgroup$
– Ismael Miguel
Mar 13 at 1:38
add a comment |
$begingroup$
Brachylog, 4 bytes
fk+?
Try it online!
The predicate succeeds for perfect inputs and fails for imperfect inputs, printing true.
or false.
if run as a complete program (except on the last test case which takes more than a minute on TIO).
The input's
f factors
k without the last element
+ sum to
? the input.
$endgroup$
Brachylog, 4 bytes
fk+?
Try it online!
The predicate succeeds for perfect inputs and fails for imperfect inputs, printing true.
or false.
if run as a complete program (except on the last test case which takes more than a minute on TIO).
The input's
f factors
k without the last element
+ sum to
? the input.
answered Mar 12 at 4:00
Unrelated StringUnrelated String
1,681312
1,681312
1
$begingroup$
I like how the code saysfk
:x
$endgroup$
– Ismael Miguel
Mar 13 at 1:38
add a comment |
1
$begingroup$
I like how the code saysfk
:x
$endgroup$
– Ismael Miguel
Mar 13 at 1:38
1
1
$begingroup$
I like how the code says
fk
:x$endgroup$
– Ismael Miguel
Mar 13 at 1:38
$begingroup$
I like how the code says
fk
:x$endgroup$
– Ismael Miguel
Mar 13 at 1:38
add a comment |
$begingroup$
Neim, 3 bytes
𝐕𝐬𝔼
Try it online!
(I don't actually know how to run all of the test cases at once, since I started learning Neim about fifteen minutes ago, but I did check them individually.)
Prints 0 for imperfect, 1 for perfect.
𝐕 Pop an int from the stack and push its proper divisors,
implicitly reading the int from a line of input as the otherwise absent top of the stack.
𝐬 Pop a list from the stack and push the sum of the values it contains.
𝔼 Pop two ints from the stack and push 1 if they are equal, 0 if they are not;
implicitly reading the same line of input that was already read as the second int, I guess?
Implicitly print the contents of the stack, or something like that.
$endgroup$
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is𝔼
1 byte? Does Neim use only 128 such non-standart characters?
$endgroup$
– kajacx
Mar 13 at 13:39
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
add a comment |
$begingroup$
Neim, 3 bytes
𝐕𝐬𝔼
Try it online!
(I don't actually know how to run all of the test cases at once, since I started learning Neim about fifteen minutes ago, but I did check them individually.)
Prints 0 for imperfect, 1 for perfect.
𝐕 Pop an int from the stack and push its proper divisors,
implicitly reading the int from a line of input as the otherwise absent top of the stack.
𝐬 Pop a list from the stack and push the sum of the values it contains.
𝔼 Pop two ints from the stack and push 1 if they are equal, 0 if they are not;
implicitly reading the same line of input that was already read as the second int, I guess?
Implicitly print the contents of the stack, or something like that.
$endgroup$
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is𝔼
1 byte? Does Neim use only 128 such non-standart characters?
$endgroup$
– kajacx
Mar 13 at 13:39
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
add a comment |
$begingroup$
Neim, 3 bytes
𝐕𝐬𝔼
Try it online!
(I don't actually know how to run all of the test cases at once, since I started learning Neim about fifteen minutes ago, but I did check them individually.)
Prints 0 for imperfect, 1 for perfect.
𝐕 Pop an int from the stack and push its proper divisors,
implicitly reading the int from a line of input as the otherwise absent top of the stack.
𝐬 Pop a list from the stack and push the sum of the values it contains.
𝔼 Pop two ints from the stack and push 1 if they are equal, 0 if they are not;
implicitly reading the same line of input that was already read as the second int, I guess?
Implicitly print the contents of the stack, or something like that.
$endgroup$
Neim, 3 bytes
𝐕𝐬𝔼
Try it online!
(I don't actually know how to run all of the test cases at once, since I started learning Neim about fifteen minutes ago, but I did check them individually.)
Prints 0 for imperfect, 1 for perfect.
𝐕 Pop an int from the stack and push its proper divisors,
implicitly reading the int from a line of input as the otherwise absent top of the stack.
𝐬 Pop a list from the stack and push the sum of the values it contains.
𝔼 Pop two ints from the stack and push 1 if they are equal, 0 if they are not;
implicitly reading the same line of input that was already read as the second int, I guess?
Implicitly print the contents of the stack, or something like that.
answered Mar 12 at 4:27
Unrelated StringUnrelated String
1,681312
1,681312
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is𝔼
1 byte? Does Neim use only 128 such non-standart characters?
$endgroup$
– kajacx
Mar 13 at 13:39
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
add a comment |
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is𝔼
1 byte? Does Neim use only 128 such non-standart characters?
$endgroup$
– kajacx
Mar 13 at 13:39
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
2
2
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
"I guess?"; "or something like that.". When you're not even sure what you've written yourself, haha. ;) But yes, that's indeed how it works. I don't know Neim, but using the input implicitly like that and outputting implicitly at the end implicitly, is similar in 05AB1E.
$endgroup$
– Kevin Cruijssen
Mar 13 at 12:26
$begingroup$
How is
𝔼
1 byte? Does Neim use only 128 such non-standart characters?$endgroup$
– kajacx
Mar 13 at 13:39
$begingroup$
How is
𝔼
1 byte? Does Neim use only 128 such non-standart characters?$endgroup$
– kajacx
Mar 13 at 13:39
3
3
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
$begingroup$
@kajacx Neim has its own code page. Therefore, each of the 256 characters present in the codepage can be encoded using 1 byte.
$endgroup$
– Mr. Xcoder
Mar 13 at 17:23
add a comment |
$begingroup$
R, 33 29 bytes
!2*(n=scan())-(x=1:n)%*%!n%%x
Try it online!
Returns TRUE
for perfect numbers and FALSE
for imperfect ones.
$endgroup$
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped0
(perfect) toFALSE
and nonzero toTRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast fromnumeric
tological
, often in conjunction withwhich
or[
.
$endgroup$
– Giuseppe
Mar 12 at 3:37
add a comment |
$begingroup$
R, 33 29 bytes
!2*(n=scan())-(x=1:n)%*%!n%%x
Try it online!
Returns TRUE
for perfect numbers and FALSE
for imperfect ones.
$endgroup$
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped0
(perfect) toFALSE
and nonzero toTRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast fromnumeric
tological
, often in conjunction withwhich
or[
.
$endgroup$
– Giuseppe
Mar 12 at 3:37
add a comment |
$begingroup$
R, 33 29 bytes
!2*(n=scan())-(x=1:n)%*%!n%%x
Try it online!
Returns TRUE
for perfect numbers and FALSE
for imperfect ones.
$endgroup$
R, 33 29 bytes
!2*(n=scan())-(x=1:n)%*%!n%%x
Try it online!
Returns TRUE
for perfect numbers and FALSE
for imperfect ones.
edited Mar 12 at 13:22
answered Mar 12 at 3:13
GiuseppeGiuseppe
17.8k31153
17.8k31153
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped0
(perfect) toFALSE
and nonzero toTRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast fromnumeric
tological
, often in conjunction withwhich
or[
.
$endgroup$
– Giuseppe
Mar 12 at 3:37
add a comment |
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped0
(perfect) toFALSE
and nonzero toTRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast fromnumeric
tological
, often in conjunction withwhich
or[
.
$endgroup$
– Giuseppe
Mar 12 at 3:37
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
What do the 2 !s in a row get you?
$endgroup$
– CT Hall
Mar 12 at 3:16
$begingroup$
@CTHall I misread the spec; they originally mapped
0
(perfect) to FALSE
and nonzero to TRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast from numeric
to logical
, often in conjunction with which
or [
.$endgroup$
– Giuseppe
Mar 12 at 3:37
$begingroup$
@CTHall I misread the spec; they originally mapped
0
(perfect) to FALSE
and nonzero to TRUE
but I removed one of them to reverse the mapping. It's a useful golfing trick to cast from numeric
to logical
, often in conjunction with which
or [
.$endgroup$
– Giuseppe
Mar 12 at 3:37
add a comment |
$begingroup$
Jelly, 3 bytes
Æṣ=
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 3 bytes
Æṣ=
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 3 bytes
Æṣ=
Try it online!
$endgroup$
Jelly, 3 bytes
Æṣ=
Try it online!
answered Mar 12 at 4:21
Mr. XcoderMr. Xcoder
32.3k759200
32.3k759200
add a comment |
add a comment |
$begingroup$
Japt -!
, 4 bytes
¥â¬x
-----------------
Implicit Input U
¥ Equal to
x Sum of
â Factors of U
¬ Without itself
For some reason ¦
doesnt work on tio so I need to use the -!
flag and ¥
instead
Try it online!
$endgroup$
$begingroup$
That's not a TIO issue;U
doesn't get auto-inserted before!
.
$endgroup$
– Shaggy
Mar 12 at 9:31
add a comment |
$begingroup$
Japt -!
, 4 bytes
¥â¬x
-----------------
Implicit Input U
¥ Equal to
x Sum of
â Factors of U
¬ Without itself
For some reason ¦
doesnt work on tio so I need to use the -!
flag and ¥
instead
Try it online!
$endgroup$
$begingroup$
That's not a TIO issue;U
doesn't get auto-inserted before!
.
$endgroup$
– Shaggy
Mar 12 at 9:31
add a comment |
$begingroup$
Japt -!
, 4 bytes
¥â¬x
-----------------
Implicit Input U
¥ Equal to
x Sum of
â Factors of U
¬ Without itself
For some reason ¦
doesnt work on tio so I need to use the -!
flag and ¥
instead
Try it online!
$endgroup$
Japt -!
, 4 bytes
¥â¬x
-----------------
Implicit Input U
¥ Equal to
x Sum of
â Factors of U
¬ Without itself
For some reason ¦
doesnt work on tio so I need to use the -!
flag and ¥
instead
Try it online!
edited Mar 13 at 22:48
answered Mar 12 at 2:53
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,72821671
5,72821671
$begingroup$
That's not a TIO issue;U
doesn't get auto-inserted before!
.
$endgroup$
– Shaggy
Mar 12 at 9:31
add a comment |
$begingroup$
That's not a TIO issue;U
doesn't get auto-inserted before!
.
$endgroup$
– Shaggy
Mar 12 at 9:31
$begingroup$
That's not a TIO issue;
U
doesn't get auto-inserted before !
.$endgroup$
– Shaggy
Mar 12 at 9:31
$begingroup$
That's not a TIO issue;
U
doesn't get auto-inserted before !
.$endgroup$
– Shaggy
Mar 12 at 9:31
add a comment |
$begingroup$
Python 3, 46 bytes
lambda x:sum(i for i in range(1,x)if x%i<1)==x
Try it online!
Brute force, sums the factors and checks for equality.
$endgroup$
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.
$endgroup$
– nedla2004
Mar 12 at 13:13
add a comment |
$begingroup$
Python 3, 46 bytes
lambda x:sum(i for i in range(1,x)if x%i<1)==x
Try it online!
Brute force, sums the factors and checks for equality.
$endgroup$
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.
$endgroup$
– nedla2004
Mar 12 at 13:13
add a comment |
$begingroup$
Python 3, 46 bytes
lambda x:sum(i for i in range(1,x)if x%i<1)==x
Try it online!
Brute force, sums the factors and checks for equality.
$endgroup$
Python 3, 46 bytes
lambda x:sum(i for i in range(1,x)if x%i<1)==x
Try it online!
Brute force, sums the factors and checks for equality.
edited Mar 12 at 3:58
answered Mar 12 at 3:57
Neil A.Neil A.
1,358120
1,358120
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.
$endgroup$
– nedla2004
Mar 12 at 13:13
add a comment |
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.
$endgroup$
– nedla2004
Mar 12 at 13:13
2
2
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Using the comprehension condition as a mask for your iteration variable would save a byte.
$endgroup$
– Jonathan Frech
Mar 12 at 4:04
$begingroup$
Since you can return truthy for an imperfect number,
lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.$endgroup$
– nedla2004
Mar 12 at 13:13
$begingroup$
Since you can return truthy for an imperfect number,
lambda x:sum(i for i in range(1,x)if x%i<1)^x
should work as well.$endgroup$
– nedla2004
Mar 12 at 13:13
add a comment |
$begingroup$
Python, 45 bytes
lambda n:sum(d*(n%d<1)for d in range(1,n))==n
True
for perfect; False
for others (switch this with ==
-> !=
)
Try it online!
44 42 41 bytes (-2 thanks to ovs) if we may output using "truthy vs falsey":
f=lambda n,i=1:i/n or-~f(n,i+1)-(n%i<1)*i
(falsey (0
)) for perfect; truthy (a non-zero integer) otherwise
$endgroup$
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
add a comment |
$begingroup$
Python, 45 bytes
lambda n:sum(d*(n%d<1)for d in range(1,n))==n
True
for perfect; False
for others (switch this with ==
-> !=
)
Try it online!
44 42 41 bytes (-2 thanks to ovs) if we may output using "truthy vs falsey":
f=lambda n,i=1:i/n or-~f(n,i+1)-(n%i<1)*i
(falsey (0
)) for perfect; truthy (a non-zero integer) otherwise
$endgroup$
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
add a comment |
$begingroup$
Python, 45 bytes
lambda n:sum(d*(n%d<1)for d in range(1,n))==n
True
for perfect; False
for others (switch this with ==
-> !=
)
Try it online!
44 42 41 bytes (-2 thanks to ovs) if we may output using "truthy vs falsey":
f=lambda n,i=1:i/n or-~f(n,i+1)-(n%i<1)*i
(falsey (0
)) for perfect; truthy (a non-zero integer) otherwise
$endgroup$
Python, 45 bytes
lambda n:sum(d*(n%d<1)for d in range(1,n))==n
True
for perfect; False
for others (switch this with ==
-> !=
)
Try it online!
44 42 41 bytes (-2 thanks to ovs) if we may output using "truthy vs falsey":
f=lambda n,i=1:i/n or-~f(n,i+1)-(n%i<1)*i
(falsey (0
)) for perfect; truthy (a non-zero integer) otherwise
edited Mar 12 at 12:15
answered Mar 12 at 10:05
Jonathan AllanJonathan Allan
54.4k537174
54.4k537174
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
add a comment |
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
If the second output format is valid, this can be done in 42 bytes.
$endgroup$
– ovs
Mar 12 at 11:43
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ah, nicely done.
$endgroup$
– Jonathan Allan
Mar 12 at 12:06
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
$begingroup$
@ovs ..and another saved from that - thanks!
$endgroup$
– Jonathan Allan
Mar 12 at 12:16
add a comment |
$begingroup$
Octave, 25 bytes
@(n)~mod(n,t=1:n)*t'==2*n
Try it online!
Explanation
@(n)~mod(n,t=1:n)*t'==2*n
@(n) % Define anonymous function with input n
1:n % Row vector [1,2,...,n]
t= % Store in variable t
mod(n, ) % n modulo [1,2,...,n], element-wise. Gives 0 for divisors
~ % Logical negate. Gives 1 for divisors
t' % t transposed. Gives column vector [1;2;...;n]
* % Matrix multiply
2*n % Input times 2
== % Equal? This is the output value
$endgroup$
add a comment |
$begingroup$
Octave, 25 bytes
@(n)~mod(n,t=1:n)*t'==2*n
Try it online!
Explanation
@(n)~mod(n,t=1:n)*t'==2*n
@(n) % Define anonymous function with input n
1:n % Row vector [1,2,...,n]
t= % Store in variable t
mod(n, ) % n modulo [1,2,...,n], element-wise. Gives 0 for divisors
~ % Logical negate. Gives 1 for divisors
t' % t transposed. Gives column vector [1;2;...;n]
* % Matrix multiply
2*n % Input times 2
== % Equal? This is the output value
$endgroup$
add a comment |
$begingroup$
Octave, 25 bytes
@(n)~mod(n,t=1:n)*t'==2*n
Try it online!
Explanation
@(n)~mod(n,t=1:n)*t'==2*n
@(n) % Define anonymous function with input n
1:n % Row vector [1,2,...,n]
t= % Store in variable t
mod(n, ) % n modulo [1,2,...,n], element-wise. Gives 0 for divisors
~ % Logical negate. Gives 1 for divisors
t' % t transposed. Gives column vector [1;2;...;n]
* % Matrix multiply
2*n % Input times 2
== % Equal? This is the output value
$endgroup$
Octave, 25 bytes
@(n)~mod(n,t=1:n)*t'==2*n
Try it online!
Explanation
@(n)~mod(n,t=1:n)*t'==2*n
@(n) % Define anonymous function with input n
1:n % Row vector [1,2,...,n]
t= % Store in variable t
mod(n, ) % n modulo [1,2,...,n], element-wise. Gives 0 for divisors
~ % Logical negate. Gives 1 for divisors
t' % t transposed. Gives column vector [1;2;...;n]
* % Matrix multiply
2*n % Input times 2
== % Equal? This is the output value
edited Mar 12 at 17:22
answered Mar 12 at 9:36
Luis MendoLuis Mendo
75.3k889292
75.3k889292
add a comment |
add a comment |
$begingroup$
JavaScript, 38 bytes
n=>eval("for(i=s=n;i--;)n%i||!(s-=i)")
Try it online!
(Last testcase timeout on TIO.)
$endgroup$
$begingroup$
@Arnauld Just forgot to remove thef=
after converting from a recursive function.
$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
add a comment |
$begingroup$
JavaScript, 38 bytes
n=>eval("for(i=s=n;i--;)n%i||!(s-=i)")
Try it online!
(Last testcase timeout on TIO.)
$endgroup$
$begingroup$
@Arnauld Just forgot to remove thef=
after converting from a recursive function.
$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
add a comment |
$begingroup$
JavaScript, 38 bytes
n=>eval("for(i=s=n;i--;)n%i||!(s-=i)")
Try it online!
(Last testcase timeout on TIO.)
$endgroup$
JavaScript, 38 bytes
n=>eval("for(i=s=n;i--;)n%i||!(s-=i)")
Try it online!
(Last testcase timeout on TIO.)
edited Mar 12 at 10:06
answered Mar 12 at 6:33
tshtsh
9,50011654
9,50011654
$begingroup$
@Arnauld Just forgot to remove thef=
after converting from a recursive function.
$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
add a comment |
$begingroup$
@Arnauld Just forgot to remove thef=
after converting from a recursive function.
$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
$begingroup$
@Arnauld Just forgot to remove the
f=
after converting from a recursive function.$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
@Arnauld Just forgot to remove the
f=
after converting from a recursive function.$endgroup$
– tsh
Mar 12 at 10:07
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
Just out of curiosity, why not going with a recursive version? (It would be 34 bytes.)
$endgroup$
– Arnauld
Mar 12 at 10:09
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
$begingroup$
@Arnauld because recursive version would simply failed for larger testcase due to stack overflow. Maybe I need some environments default to strict mode to make it work.
$endgroup$
– tsh
Mar 12 at 10:17
2
2
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
$begingroup$
Fair enough, but your program doesn't have to complete the larger test cases (which I think is the default rule, anyway).
$endgroup$
– Arnauld
Mar 12 at 10:22
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 46 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)^n*2
Returns 0 if perfect, otherwise returns a positive number. I don't know if outputting different types of integers are allowed in place of two distinct truthy and falsy values, and couldn't find any discussion on meta about it. If this is invalid, I will remove it.
Try it online!
C# (Visual C# Interactive Compiler), 49 47 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)==n*2
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 46 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)^n*2
Returns 0 if perfect, otherwise returns a positive number. I don't know if outputting different types of integers are allowed in place of two distinct truthy and falsy values, and couldn't find any discussion on meta about it. If this is invalid, I will remove it.
Try it online!
C# (Visual C# Interactive Compiler), 49 47 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)==n*2
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 46 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)^n*2
Returns 0 if perfect, otherwise returns a positive number. I don't know if outputting different types of integers are allowed in place of two distinct truthy and falsy values, and couldn't find any discussion on meta about it. If this is invalid, I will remove it.
Try it online!
C# (Visual C# Interactive Compiler), 49 47 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)==n*2
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 46 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)^n*2
Returns 0 if perfect, otherwise returns a positive number. I don't know if outputting different types of integers are allowed in place of two distinct truthy and falsy values, and couldn't find any discussion on meta about it. If this is invalid, I will remove it.
Try it online!
C# (Visual C# Interactive Compiler), 49 47 bytes
n=>Enumerable.Range(1,n).Sum(x=>n%x<1?x:0)==n*2
Try it online!
edited Mar 13 at 3:51
answered Mar 12 at 3:53
Embodiment of IgnoranceEmbodiment of Ignorance
2,956127
2,956127
add a comment |
add a comment |
$begingroup$
Ruby, 33 bytes
->n{(1...n).sum{|i|n%i<1?i:0}==n}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 33 bytes
->n{(1...n).sum{|i|n%i<1?i:0}==n}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 33 bytes
->n{(1...n).sum{|i|n%i<1?i:0}==n}
Try it online!
$endgroup$
Ruby, 33 bytes
->n{(1...n).sum{|i|n%i<1?i:0}==n}
Try it online!
answered Mar 12 at 8:10
Kirill L.Kirill L.
6,2181528
6,2181528
add a comment |
add a comment |
$begingroup$
TI-BASIC (TI-84), 30 23 bytes
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1
Horribly inefficient, but it works.
Reducing the bytecount sped up the program by a lot.
Input is in Ans
.
Output is in Ans
and is automatically printed out when the program completes.
Explanation:
(TI-BASIC doesn't have comments, so just assume that ;
makes a comment)
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans ;Full program
2Ans ;double the input
seq( ;generate a list
X, ;using the variable X,
1, ;starting at 1,
Ans ;and ending at the input
;with an implied increment of 1
Ans/X ;from the input divided by X
not( ), ;multiplied by the negated result of
remainder(Ans,X) ;the input modulo X
;(result: 0 or 1)
sum( ;sum up the elements in the list
= ;equal?
Example:
6
6
prgmCDGF2
1
7
7
prgmCDGF2
0
Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2
, (5 bytes) and an extra 8 bytes used for storing the program:
36 - 5 - 8 = 23 bytes
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 30 23 bytes
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1
Horribly inefficient, but it works.
Reducing the bytecount sped up the program by a lot.
Input is in Ans
.
Output is in Ans
and is automatically printed out when the program completes.
Explanation:
(TI-BASIC doesn't have comments, so just assume that ;
makes a comment)
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans ;Full program
2Ans ;double the input
seq( ;generate a list
X, ;using the variable X,
1, ;starting at 1,
Ans ;and ending at the input
;with an implied increment of 1
Ans/X ;from the input divided by X
not( ), ;multiplied by the negated result of
remainder(Ans,X) ;the input modulo X
;(result: 0 or 1)
sum( ;sum up the elements in the list
= ;equal?
Example:
6
6
prgmCDGF2
1
7
7
prgmCDGF2
0
Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2
, (5 bytes) and an extra 8 bytes used for storing the program:
36 - 5 - 8 = 23 bytes
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 30 23 bytes
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1
Horribly inefficient, but it works.
Reducing the bytecount sped up the program by a lot.
Input is in Ans
.
Output is in Ans
and is automatically printed out when the program completes.
Explanation:
(TI-BASIC doesn't have comments, so just assume that ;
makes a comment)
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans ;Full program
2Ans ;double the input
seq( ;generate a list
X, ;using the variable X,
1, ;starting at 1,
Ans ;and ending at the input
;with an implied increment of 1
Ans/X ;from the input divided by X
not( ), ;multiplied by the negated result of
remainder(Ans,X) ;the input modulo X
;(result: 0 or 1)
sum( ;sum up the elements in the list
= ;equal?
Example:
6
6
prgmCDGF2
1
7
7
prgmCDGF2
0
Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2
, (5 bytes) and an extra 8 bytes used for storing the program:
36 - 5 - 8 = 23 bytes
$endgroup$
TI-BASIC (TI-84), 30 23 bytes
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1
Horribly inefficient, but it works.
Reducing the bytecount sped up the program by a lot.
Input is in Ans
.
Output is in Ans
and is automatically printed out when the program completes.
Explanation:
(TI-BASIC doesn't have comments, so just assume that ;
makes a comment)
:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans ;Full program
2Ans ;double the input
seq( ;generate a list
X, ;using the variable X,
1, ;starting at 1,
Ans ;and ending at the input
;with an implied increment of 1
Ans/X ;from the input divided by X
not( ), ;multiplied by the negated result of
remainder(Ans,X) ;the input modulo X
;(result: 0 or 1)
sum( ;sum up the elements in the list
= ;equal?
Example:
6
6
prgmCDGF2
1
7
7
prgmCDGF2
0
Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2
, (5 bytes) and an extra 8 bytes used for storing the program:
36 - 5 - 8 = 23 bytes
edited Mar 13 at 16:34
answered Mar 12 at 13:36
TauTau
1,063519
1,063519
add a comment |
add a comment |
$begingroup$
Java (JDK), 54 bytes
n->{int s=0,d=0;for(;++d<n;)s+=n%d<1?d:0;return s==n;}
Try it online!
Though for a strict number by number matching, the following will return the same values, but is only 40 bytes.
n->n==6|n==28|n==496|n==8128|n==33550336
Try it online!
$endgroup$
$begingroup$
The rules sayYour program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Javaint
at all, but rather aBigInteger
? Because Java hasBigIntegers
, but it won't ever have anint
that's more than 31 bits as signed, which can't hold any other value than those represented here...
$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if theint
type was unbounded
$endgroup$
– Jo King
Mar 13 at 21:16
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
add a comment |
$begingroup$
Java (JDK), 54 bytes
n->{int s=0,d=0;for(;++d<n;)s+=n%d<1?d:0;return s==n;}
Try it online!
Though for a strict number by number matching, the following will return the same values, but is only 40 bytes.
n->n==6|n==28|n==496|n==8128|n==33550336
Try it online!
$endgroup$
$begingroup$
The rules sayYour program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Javaint
at all, but rather aBigInteger
? Because Java hasBigIntegers
, but it won't ever have anint
that's more than 31 bits as signed, which can't hold any other value than those represented here...
$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if theint
type was unbounded
$endgroup$
– Jo King
Mar 13 at 21:16
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
add a comment |
$begingroup$
Java (JDK), 54 bytes
n->{int s=0,d=0;for(;++d<n;)s+=n%d<1?d:0;return s==n;}
Try it online!
Though for a strict number by number matching, the following will return the same values, but is only 40 bytes.
n->n==6|n==28|n==496|n==8128|n==33550336
Try it online!
$endgroup$
Java (JDK), 54 bytes
n->{int s=0,d=0;for(;++d<n;)s+=n%d<1?d:0;return s==n;}
Try it online!
Though for a strict number by number matching, the following will return the same values, but is only 40 bytes.
n->n==6|n==28|n==496|n==8128|n==33550336
Try it online!
edited Mar 13 at 22:34
answered Mar 12 at 9:28
Olivier GrégoireOlivier Grégoire
9,41511944
9,41511944
$begingroup$
The rules sayYour program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Javaint
at all, but rather aBigInteger
? Because Java hasBigIntegers
, but it won't ever have anint
that's more than 31 bits as signed, which can't hold any other value than those represented here...
$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if theint
type was unbounded
$endgroup$
– Jo King
Mar 13 at 21:16
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
add a comment |
$begingroup$
The rules sayYour program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Javaint
at all, but rather aBigInteger
? Because Java hasBigIntegers
, but it won't ever have anint
that's more than 31 bits as signed, which can't hold any other value than those represented here...
$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if theint
type was unbounded
$endgroup$
– Jo King
Mar 13 at 21:16
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
$begingroup$
The rules say
Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
The rules say
Your program doesn't have to complete the larger test cases, if there's memory or time constraints, but it should be theoretically able to if it were given more memory/time.
$endgroup$
– Jo King
Mar 13 at 14:28
$begingroup$
@JoKing Does that mean that I can't use a Java
int
at all, but rather a BigInteger
? Because Java has BigIntegers
, but it won't ever have an int
that's more than 31 bits as signed, which can't hold any other value than those represented here...$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
@JoKing Does that mean that I can't use a Java
int
at all, but rather a BigInteger
? Because Java has BigIntegers
, but it won't ever have an int
that's more than 31 bits as signed, which can't hold any other value than those represented here...$endgroup$
– Olivier Grégoire
Mar 13 at 14:30
$begingroup$
no, but if the program should still work if the
int
type was unbounded$endgroup$
– Jo King
Mar 13 at 21:16
$begingroup$
no, but if the program should still work if the
int
type was unbounded$endgroup$
– Jo King
Mar 13 at 21:16
1
1
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
$begingroup$
@JoKing Ok, I switched the two solutions again to have the computation first.
$endgroup$
– Olivier Grégoire
Mar 13 at 22:35
add a comment |
$begingroup$
x86 Assembly, 45 43 Bytes.
6A 00 31 C9 31 D2 41 39 C1 7D 0B 50 F7 F9 58 85
D2 75 F1 51 EB EE 31 D2 59 01 CA 85 C9 75 F9 39
D0 75 05 31 C0 40 EB 02 31 C0 C3
Explaination (Intel Syntax):
PUSH $0 ; Terminator for later
XOR ECX, ECX ; Clear ECX
.factor:
XOR EDX, EDX ; Clear EDX
INC ECX
CMP ECX, EAX ; divisor >= input number?
JGE .factordone ; if so, exit loop.
PUSH EAX ; backup EAX
IDIV ECX ; divide EDX:EAX by ECX, store result in EAX and remainder in EDX
POP EAX ; restore EAX
TEST EDX, EDX ; remainder == 0?
JNZ .factor ; if not, jump back to loop start
PUSH ECX ; push factor
JMP .factor ; jump back to loop start
.factordone:
XOR EDX, EDX ; clear EDX
.sum:
POP ECX ; pop divisor
ADD EDX, ECX ; sum into EDX
TEST ECX, ECX ; divisor == 0?
JNZ .sum ; if not, loop.
CMP EAX, EDX ; input number == sum?
JNE .noteq ; if not, skip to .noteq
XOR EAX, EAX ; clear EAX
INC EAX ; increment EAX (sets to 1)
JMP .return ; skip to .return
.noteq:
XOR EAX, EAX ; clear EAX
.return:
RETN
Input should be provided in EAX
.
Function sets EAX
to 1
for perfect and to 0
for imperfect.
EDIT: Reduced Byte-Count by two by replacing MOV EAX, $1
with XOR EAX, EAX
and INC EAX
$endgroup$
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
add a comment |
$begingroup$
x86 Assembly, 45 43 Bytes.
6A 00 31 C9 31 D2 41 39 C1 7D 0B 50 F7 F9 58 85
D2 75 F1 51 EB EE 31 D2 59 01 CA 85 C9 75 F9 39
D0 75 05 31 C0 40 EB 02 31 C0 C3
Explaination (Intel Syntax):
PUSH $0 ; Terminator for later
XOR ECX, ECX ; Clear ECX
.factor:
XOR EDX, EDX ; Clear EDX
INC ECX
CMP ECX, EAX ; divisor >= input number?
JGE .factordone ; if so, exit loop.
PUSH EAX ; backup EAX
IDIV ECX ; divide EDX:EAX by ECX, store result in EAX and remainder in EDX
POP EAX ; restore EAX
TEST EDX, EDX ; remainder == 0?
JNZ .factor ; if not, jump back to loop start
PUSH ECX ; push factor
JMP .factor ; jump back to loop start
.factordone:
XOR EDX, EDX ; clear EDX
.sum:
POP ECX ; pop divisor
ADD EDX, ECX ; sum into EDX
TEST ECX, ECX ; divisor == 0?
JNZ .sum ; if not, loop.
CMP EAX, EDX ; input number == sum?
JNE .noteq ; if not, skip to .noteq
XOR EAX, EAX ; clear EAX
INC EAX ; increment EAX (sets to 1)
JMP .return ; skip to .return
.noteq:
XOR EAX, EAX ; clear EAX
.return:
RETN
Input should be provided in EAX
.
Function sets EAX
to 1
for perfect and to 0
for imperfect.
EDIT: Reduced Byte-Count by two by replacing MOV EAX, $1
with XOR EAX, EAX
and INC EAX
$endgroup$
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
add a comment |
$begingroup$
x86 Assembly, 45 43 Bytes.
6A 00 31 C9 31 D2 41 39 C1 7D 0B 50 F7 F9 58 85
D2 75 F1 51 EB EE 31 D2 59 01 CA 85 C9 75 F9 39
D0 75 05 31 C0 40 EB 02 31 C0 C3
Explaination (Intel Syntax):
PUSH $0 ; Terminator for later
XOR ECX, ECX ; Clear ECX
.factor:
XOR EDX, EDX ; Clear EDX
INC ECX
CMP ECX, EAX ; divisor >= input number?
JGE .factordone ; if so, exit loop.
PUSH EAX ; backup EAX
IDIV ECX ; divide EDX:EAX by ECX, store result in EAX and remainder in EDX
POP EAX ; restore EAX
TEST EDX, EDX ; remainder == 0?
JNZ .factor ; if not, jump back to loop start
PUSH ECX ; push factor
JMP .factor ; jump back to loop start
.factordone:
XOR EDX, EDX ; clear EDX
.sum:
POP ECX ; pop divisor
ADD EDX, ECX ; sum into EDX
TEST ECX, ECX ; divisor == 0?
JNZ .sum ; if not, loop.
CMP EAX, EDX ; input number == sum?
JNE .noteq ; if not, skip to .noteq
XOR EAX, EAX ; clear EAX
INC EAX ; increment EAX (sets to 1)
JMP .return ; skip to .return
.noteq:
XOR EAX, EAX ; clear EAX
.return:
RETN
Input should be provided in EAX
.
Function sets EAX
to 1
for perfect and to 0
for imperfect.
EDIT: Reduced Byte-Count by two by replacing MOV EAX, $1
with XOR EAX, EAX
and INC EAX
$endgroup$
x86 Assembly, 45 43 Bytes.
6A 00 31 C9 31 D2 41 39 C1 7D 0B 50 F7 F9 58 85
D2 75 F1 51 EB EE 31 D2 59 01 CA 85 C9 75 F9 39
D0 75 05 31 C0 40 EB 02 31 C0 C3
Explaination (Intel Syntax):
PUSH $0 ; Terminator for later
XOR ECX, ECX ; Clear ECX
.factor:
XOR EDX, EDX ; Clear EDX
INC ECX
CMP ECX, EAX ; divisor >= input number?
JGE .factordone ; if so, exit loop.
PUSH EAX ; backup EAX
IDIV ECX ; divide EDX:EAX by ECX, store result in EAX and remainder in EDX
POP EAX ; restore EAX
TEST EDX, EDX ; remainder == 0?
JNZ .factor ; if not, jump back to loop start
PUSH ECX ; push factor
JMP .factor ; jump back to loop start
.factordone:
XOR EDX, EDX ; clear EDX
.sum:
POP ECX ; pop divisor
ADD EDX, ECX ; sum into EDX
TEST ECX, ECX ; divisor == 0?
JNZ .sum ; if not, loop.
CMP EAX, EDX ; input number == sum?
JNE .noteq ; if not, skip to .noteq
XOR EAX, EAX ; clear EAX
INC EAX ; increment EAX (sets to 1)
JMP .return ; skip to .return
.noteq:
XOR EAX, EAX ; clear EAX
.return:
RETN
Input should be provided in EAX
.
Function sets EAX
to 1
for perfect and to 0
for imperfect.
EDIT: Reduced Byte-Count by two by replacing MOV EAX, $1
with XOR EAX, EAX
and INC EAX
edited Mar 14 at 21:14
answered Mar 14 at 13:49
Fayti1703Fayti1703
613
613
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
add a comment |
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
1
1
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
I use a macro assembly so I don't know for sure but the comment"; divisor > input number" for me would be "; divisor >= input number"
$endgroup$
– RosLuP
Mar 14 at 16:54
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
Assembly has easy operations one could reduce instructions length puts all in a line, use indentation and comment every 10 20 asm instructions....
$endgroup$
– RosLuP
Mar 14 at 16:58
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
$begingroup$
@RosLuP I've fixed the comment in the code (thanks), but I don't know what you mean with your second comment.
$endgroup$
– Fayti1703
Mar 14 at 19:23
add a comment |
$begingroup$
CJam, 17 bytes
ri_,(;{1$%!},:+=
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 17 bytes
ri_,(;{1$%!},:+=
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 17 bytes
ri_,(;{1$%!},:+=
Try it online!
$endgroup$
CJam, 17 bytes
ri_,(;{1$%!},:+=
Try it online!
answered Mar 12 at 2:59
Esolanging FruitEsolanging Fruit
8,73932776
8,73932776
add a comment |
add a comment |
$begingroup$
Javascript, 62
n=>n==[...Array(n).keys()].filter(a=>n%a<1).reduce((a,b)=>a+b)
Explanation (although it's pretty simple)
n=> //return function that takes n
n== //and returns if n is equal to
[...Array(n).keys()] //an array [0..(n-1)]...
.filter(a=>n%a<1) //where all of the elements that are not divisors of n are taken out...
.reduce((a,b)=>a+b) //summed up
Thanks to Jo King for the improvement!
$endgroup$
add a comment |
$begingroup$
Javascript, 62
n=>n==[...Array(n).keys()].filter(a=>n%a<1).reduce((a,b)=>a+b)
Explanation (although it's pretty simple)
n=> //return function that takes n
n== //and returns if n is equal to
[...Array(n).keys()] //an array [0..(n-1)]...
.filter(a=>n%a<1) //where all of the elements that are not divisors of n are taken out...
.reduce((a,b)=>a+b) //summed up
Thanks to Jo King for the improvement!
$endgroup$
add a comment |
$begingroup$
Javascript, 62
n=>n==[...Array(n).keys()].filter(a=>n%a<1).reduce((a,b)=>a+b)
Explanation (although it's pretty simple)
n=> //return function that takes n
n== //and returns if n is equal to
[...Array(n).keys()] //an array [0..(n-1)]...
.filter(a=>n%a<1) //where all of the elements that are not divisors of n are taken out...
.reduce((a,b)=>a+b) //summed up
Thanks to Jo King for the improvement!
$endgroup$
Javascript, 62
n=>n==[...Array(n).keys()].filter(a=>n%a<1).reduce((a,b)=>a+b)
Explanation (although it's pretty simple)
n=> //return function that takes n
n== //and returns if n is equal to
[...Array(n).keys()] //an array [0..(n-1)]...
.filter(a=>n%a<1) //where all of the elements that are not divisors of n are taken out...
.reduce((a,b)=>a+b) //summed up
Thanks to Jo King for the improvement!
edited Mar 12 at 3:03
answered Mar 12 at 2:55
vityavvvityavv
659214
659214
add a comment |
add a comment |
$begingroup$
05AB1E, 4 bytes
ѨOQ
Try it online!
Explanation
O # the sum
Ñ # of the divisors of the input
¨ # with the last one removed
Q # equals the input
$endgroup$
add a comment |
$begingroup$
05AB1E, 4 bytes
ѨOQ
Try it online!
Explanation
O # the sum
Ñ # of the divisors of the input
¨ # with the last one removed
Q # equals the input
$endgroup$
add a comment |
$begingroup$
05AB1E, 4 bytes
ѨOQ
Try it online!
Explanation
O # the sum
Ñ # of the divisors of the input
¨ # with the last one removed
Q # equals the input
$endgroup$
05AB1E, 4 bytes
ѨOQ
Try it online!
Explanation
O # the sum
Ñ # of the divisors of the input
¨ # with the last one removed
Q # equals the input
answered Mar 12 at 7:07
EmignaEmigna
48k434146
48k434146
add a comment |
add a comment |
$begingroup$
Powershell, 46 bytes 43 bytes
param($i)1..$i|%{$o+=$_*!($i%$_)};$o-eq2*$i
Try it Online!
Edit:
-3 bytes thanks to @AdmBorkBork
$endgroup$
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against2*$i
to eliminate the subtract-one parens.
$endgroup$
– AdmBorkBork
Mar 12 at 12:42
add a comment |
$begingroup$
Powershell, 46 bytes 43 bytes
param($i)1..$i|%{$o+=$_*!($i%$_)};$o-eq2*$i
Try it Online!
Edit:
-3 bytes thanks to @AdmBorkBork
$endgroup$
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against2*$i
to eliminate the subtract-one parens.
$endgroup$
– AdmBorkBork
Mar 12 at 12:42
add a comment |
$begingroup$
Powershell, 46 bytes 43 bytes
param($i)1..$i|%{$o+=$_*!($i%$_)};$o-eq2*$i
Try it Online!
Edit:
-3 bytes thanks to @AdmBorkBork
$endgroup$
Powershell, 46 bytes 43 bytes
param($i)1..$i|%{$o+=$_*!($i%$_)};$o-eq2*$i
Try it Online!
Edit:
-3 bytes thanks to @AdmBorkBork
edited Mar 12 at 12:52
answered Mar 12 at 12:13
J. BergmannJ. Bergmann
2213
2213
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against2*$i
to eliminate the subtract-one parens.
$endgroup$
– AdmBorkBork
Mar 12 at 12:42
add a comment |
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against2*$i
to eliminate the subtract-one parens.
$endgroup$
– AdmBorkBork
Mar 12 at 12:42
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against
2*$i
to eliminate the subtract-one parens.$endgroup$
– AdmBorkBork
Mar 12 at 12:42
$begingroup$
43 bytes by rolling the accumulator into the loop and checking against
2*$i
to eliminate the subtract-one parens.$endgroup$
– AdmBorkBork
Mar 12 at 12:42
add a comment |
$begingroup$
C (gcc), 41 bytes
f(n,i,s){for(i=s=n;--i;s-=n%i?0:i);n=!s;}
Try it online!
1: 0
12: 0
13: 0
18: 0
20: 0
1000: 0
33550335: 0
6: 1
28: 1
496: 1
8128: 1
33550336: 1
-65536: 0 <---- Unable to represent final test case with four bytes, fails
Let me know if that failure for the final case is an issue.
$endgroup$
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
$begingroup$
@Neil Better yet, it can be fixed withn=!s;
instead ofreturn!s;
to save 5 bytes.
$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I dids=s
which more than likely got optimized out.
$endgroup$
– Marcos
Mar 13 at 0:26
add a comment |
$begingroup$
C (gcc), 41 bytes
f(n,i,s){for(i=s=n;--i;s-=n%i?0:i);n=!s;}
Try it online!
1: 0
12: 0
13: 0
18: 0
20: 0
1000: 0
33550335: 0
6: 1
28: 1
496: 1
8128: 1
33550336: 1
-65536: 0 <---- Unable to represent final test case with four bytes, fails
Let me know if that failure for the final case is an issue.
$endgroup$
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
$begingroup$
@Neil Better yet, it can be fixed withn=!s;
instead ofreturn!s;
to save 5 bytes.
$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I dids=s
which more than likely got optimized out.
$endgroup$
– Marcos
Mar 13 at 0:26
add a comment |
$begingroup$
C (gcc), 41 bytes
f(n,i,s){for(i=s=n;--i;s-=n%i?0:i);n=!s;}
Try it online!
1: 0
12: 0
13: 0
18: 0
20: 0
1000: 0
33550335: 0
6: 1
28: 1
496: 1
8128: 1
33550336: 1
-65536: 0 <---- Unable to represent final test case with four bytes, fails
Let me know if that failure for the final case is an issue.
$endgroup$
C (gcc), 41 bytes
f(n,i,s){for(i=s=n;--i;s-=n%i?0:i);n=!s;}
Try it online!
1: 0
12: 0
13: 0
18: 0
20: 0
1000: 0
33550335: 0
6: 1
28: 1
496: 1
8128: 1
33550336: 1
-65536: 0 <---- Unable to represent final test case with four bytes, fails
Let me know if that failure for the final case is an issue.
edited Mar 13 at 0:28
answered Mar 12 at 7:58
MarcosMarcos
1614
1614
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
$begingroup$
@Neil Better yet, it can be fixed withn=!s;
instead ofreturn!s;
to save 5 bytes.
$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I dids=s
which more than likely got optimized out.
$endgroup$
– Marcos
Mar 13 at 0:26
add a comment |
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
$begingroup$
@Neil Better yet, it can be fixed withn=!s;
instead ofreturn!s;
to save 5 bytes.
$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I dids=s
which more than likely got optimized out.
$endgroup$
– Marcos
Mar 13 at 0:26
1
1
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
$begingroup$
41 bytes
$endgroup$
– tsh
Mar 12 at 9:00
2
2
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
$begingroup$
"Output can be two distinct and consistent values through any allowed output format." You're not returning any two distinct values.
$endgroup$
– Olivier Grégoire
Mar 12 at 9:29
2
2
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
$begingroup$
@OlivierGrégoire Fortunately that can be readily fixed by replacing the space with an exclamation mark!
$endgroup$
– Neil
Mar 12 at 9:33
1
1
$begingroup$
@Neil Better yet, it can be fixed with
n=!s;
instead of return!s;
to save 5 bytes.$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@Neil Better yet, it can be fixed with
n=!s;
instead of return!s;
to save 5 bytes.$endgroup$
– Rogem
Mar 12 at 9:50
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I did
s=s
which more than likely got optimized out.$endgroup$
– Marcos
Mar 13 at 0:26
$begingroup$
@OlivierGrégoire ahh, I forgot that point. Also, updated the with the improved code. I tried something similar, but the idiot I am I did
s=s
which more than likely got optimized out.$endgroup$
– Marcos
Mar 13 at 0:26
add a comment |
$begingroup$
Smalltalk, 34 bytes
((1to:n-1)select:[:i|n\i=0])sum=n
$endgroup$
add a comment |
$begingroup$
Smalltalk, 34 bytes
((1to:n-1)select:[:i|n\i=0])sum=n
$endgroup$
add a comment |
$begingroup$
Smalltalk, 34 bytes
((1to:n-1)select:[:i|n\i=0])sum=n
$endgroup$
Smalltalk, 34 bytes
((1to:n-1)select:[:i|n\i=0])sum=n
edited Mar 13 at 2:50
answered Mar 13 at 2:44
Leandro CanigliaLeandro Caniglia
1813
1813
add a comment |
add a comment |
$begingroup$
Forth (gforth), 45 bytes
: f 0 over 1 ?do over i mod 0= i * - loop = ;
Try it online!
Explanation
Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum equals n
Code Explanation
: f start word definition
0 over 1 create a value to hold the sum and setup the bounds of the loop
?do start a counted loop from 1 to n. (?do skips if start = end)
over copy n to the top of the stack
i mod 0= check if i divides n perfectly
i * - if so, use the fact that -1 = true in forth to add i to the sum
loop end the counted loop
= check if the sum and n are equal
; end the word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 45 bytes
: f 0 over 1 ?do over i mod 0= i * - loop = ;
Try it online!
Explanation
Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum equals n
Code Explanation
: f start word definition
0 over 1 create a value to hold the sum and setup the bounds of the loop
?do start a counted loop from 1 to n. (?do skips if start = end)
over copy n to the top of the stack
i mod 0= check if i divides n perfectly
i * - if so, use the fact that -1 = true in forth to add i to the sum
loop end the counted loop
= check if the sum and n are equal
; end the word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 45 bytes
: f 0 over 1 ?do over i mod 0= i * - loop = ;
Try it online!
Explanation
Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum equals n
Code Explanation
: f start word definition
0 over 1 create a value to hold the sum and setup the bounds of the loop
?do start a counted loop from 1 to n. (?do skips if start = end)
over copy n to the top of the stack
i mod 0= check if i divides n perfectly
i * - if so, use the fact that -1 = true in forth to add i to the sum
loop end the counted loop
= check if the sum and n are equal
; end the word definition
$endgroup$
Forth (gforth), 45 bytes
: f 0 over 1 ?do over i mod 0= i * - loop = ;
Try it online!
Explanation
Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum equals n
Code Explanation
: f start word definition
0 over 1 create a value to hold the sum and setup the bounds of the loop
?do start a counted loop from 1 to n. (?do skips if start = end)
over copy n to the top of the stack
i mod 0= check if i divides n perfectly
i * - if so, use the fact that -1 = true in forth to add i to the sum
loop end the counted loop
= check if the sum and n are equal
; end the word definition
edited Mar 13 at 12:18
answered Mar 12 at 12:35
reffureffu
75126
75126
add a comment |
add a comment |
$begingroup$
Pyth, 9 13 bytes
qsf!%QTSt
Try it online!
Thank you to the commentors for the golf help
Finds all the factors of the input, sums them, and compares that to the original input.
$endgroup$
$begingroup$
A few golfs for you -q0
can be replaced with!
, andSQ
produces the range[1-Q]
, so the range[1-Q)
can be generated usingStQ
. As theQ
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes -qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
add a comment |
$begingroup$
Pyth, 9 13 bytes
qsf!%QTSt
Try it online!
Thank you to the commentors for the golf help
Finds all the factors of the input, sums them, and compares that to the original input.
$endgroup$
$begingroup$
A few golfs for you -q0
can be replaced with!
, andSQ
produces the range[1-Q]
, so the range[1-Q)
can be generated usingStQ
. As theQ
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes -qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
add a comment |
$begingroup$
Pyth, 9 13 bytes
qsf!%QTSt
Try it online!
Thank you to the commentors for the golf help
Finds all the factors of the input, sums them, and compares that to the original input.
$endgroup$
Pyth, 9 13 bytes
qsf!%QTSt
Try it online!
Thank you to the commentors for the golf help
Finds all the factors of the input, sums them, and compares that to the original input.
edited Mar 14 at 0:26
answered Mar 12 at 7:44
JPeroutekJPeroutek
37018
37018
$begingroup$
A few golfs for you -q0
can be replaced with!
, andSQ
produces the range[1-Q]
, so the range[1-Q)
can be generated usingStQ
. As theQ
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes -qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
add a comment |
$begingroup$
A few golfs for you -q0
can be replaced with!
, andSQ
produces the range[1-Q]
, so the range[1-Q)
can be generated usingStQ
. As theQ
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes -qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
$begingroup$
A few golfs for you -
q0
can be replaced with !
, and SQ
produces the range [1-Q]
, so the range [1-Q)
can be generated using StQ
. As the Q
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes - qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
$begingroup$
A few golfs for you -
q0
can be replaced with !
, and SQ
produces the range [1-Q]
, so the range [1-Q)
can be generated using StQ
. As the Q
s are now at the end of the program they can both be omitted. Fettled version, 9 bytes - qsf!%QTSt
$endgroup$
– Sok
Mar 12 at 10:35
add a comment |
$begingroup$
Labyrinth, 80 bytes
?::`}:("(!@
perfect:
{:{:;%"}
+puts; "
}zero: "
}else{(:
"negI" _~
""""""{{{"!@
The Latin characters perfect puts zero else neg I
are actually just comments*.
i.e. if the input is perfect a 0
is printed, otherwise -1
is.
Try it online!
* so this or this work too...
?::`}:("(!@ ?::`}:("(!@
: BEWARE :
{:{:;%"} {:{:;%"}
+ ; " +LAIR; "
} : " } OF : "
} {(: }MINO{(:
" " _~ "TAUR" _~
""""""{{{"!@ """"""{{{"!@
How?
Takes as an input a positive integer n
and places an accumulator variable of -n
onto the auxiliary stack, then performs a divisibility test for each integer from n-1
down to, and including, 1
, adding any which do divide n
to the accumulator. Once this is complete if the accumulator variable is non-zero a -1
is output, otherwise a 0
is.
The ?::`}:(
is only executed once, at the beginning of execution:
?::`}:( Main,Aux
? - take an integer from STDIN and place it onto Main [[n],]
: - duplicate top of Main [[n,n],]
: - duplicate top of Main [[n,n,n],]
` - negate top of Main [[n,n,-n],]
} - place top of Main onto Aux [[n,n],[-n]]
: - duplicate top of Main [[n,n,n],[-n]]
( - decrement top of Main [[n,n,n-1],[-n]]
The next instruction, "
, is a no-op, but we have three neighbouring instructions so we branch according to the value at the top of Main, zero takes us forward, while non-zero takes us right.
If the input was 1
we go forward because the top of Main is zero:
(!@ Main,Aux
( - decrement top of Main [[1,1,-1],[-1]]
! - print top of Main, a -1
@ - exit the labyrinth
But if the input was greater than 1
we turn right because the top of Main is non-zero:
:} Main,Aux
: - duplicate top of Main [[n,n,n-1,n-1],[-n]]
} - place top of Main onto Aux [[n,n,n-1],[-n,n-1]]
At this point we have a three-neighbour branch, but we know n-1
is non-zero, so we turn right...
"% Main,Aux
" - no-op [[n,n,n-1],[-n,n-1]]
% - place modulo result onto Main [[n,n%(n-1)],[-n,n-1]]
- ...i.e we've got our first divisibility indicator n%(n-1), an
- accumulator, a=-n, and our potential divisor p=n-1:
- [[n,n%(n-1)],[a,p]]
We are now at another three-neighbour branch at %
.
If the result of %
was non-zero we go left to decrement our potential divisor, p=p-1
, and leave the accumulator, a
, as it is:
;:{(:""}" Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
- three-neighbour branch but n-1 is non-zero so we turn left
( - decrement top of Main [[n,n,p-1],[a]]
: - duplicate top of Main [[n,n,p-1,p-1],[a]]
"" - no-ops [[n,n,p-1,p-1],[a]]
} - place top of Main onto Aux [[n,n,p-1],[a,p-1]]
" - no-op [[n,n,p-1],[a,p-1]]
% - place modulo result onto Main [[n,n%(p-1)],[a,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if the result of %
was zero (for the first pass only when n=2
) we go straight on to BOTH add the divisor to our accumulator, a=a+p
, AND decrement our potential divisor, p=p-1
:
;:{:{+}}""""""""{(:""} Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
: - duplicate top of Main [[n,n,p,p],[a]]
{ - place top of Aux onto Main [[n,n,p,p,a],]
+ - perform addition [[n,n,p,a+p],]
} - place top of Main onto Aux [[n,n,p],[a+p]]
} - place top of Main onto Aux [[n,n],[a+p,p]]
""""""" - no-ops [[n,n],[a+p,p]]
- a branch, but n is non-zero so we turn left
" - no-op [[n,n],[a+p,p]]
{ - place top of Aux onto Main [[n,n,p],[a+p]]
- we branch, but p is non-zero so we turn right
( - decrement top of Main [[n,n,p-1],[a+p]]
: - duplicate top of Main [[n,n,p-1,p-1],[a+p]]
"" - no-ops [[n,n,p-1,p-1],[a+p]]
} - place top of Main onto Aux [[n,n,p-1],[a+p,p-1]]
At this point if p-1
is still non-zero we turn left:
"% Main,Aux
" - no-op [[n,n,p-1],[a+p,p-1]]
% - modulo [[n,n%(p-1)],[a+p,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if p-1
hit zero we go straight up to the :
on the second line of the labyrinth (you've seen all the instructions before, so I'm leaving their descriptions out and just giving their effect):
:":}"":({):""}"%;:{:{+}}"""""""{{{ Main,Aux
: - [[n,n,0,0],[a,0]]
" - [[n,n,0,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
: - [[n,n,0,0,0],[a,0]]
} - [[n,n,0,0],[a,0,0]]
- top of Main is zero so we go straight
"" - [[n,n,0,0],[a,0,0]]
: - [[n,n,0,0,0],[a,0,0]]
( - [[n,n,0,0,-1],[a,0,0]]
{ - [[n,n,0,0,-1,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
( - [[n,n,0,0,-1,-1],[a,0]]
: - [[n,n,0,0,-1,-1,-1],[a,0]]
"" - [[n,n,0,0,-1,-1,-1],[a,0]]
} - [[n,n,0,0,-1,-1],[a,0,-1]]
- top of Main is non-zero so we turn left
" - [[n,n,0,0,-1,-1],[a,0,-1]]
% - (-1)%(-1)=0 [[n,n,0,0,0],[a,0,-1]]
; - [[n,n,0,0],[a,0,-1]]
: - [[n,n,0,0,0],[a,0,-1]]
{ - [[n,n,0,0,0,-1],[a,0]]
: - [[n,n,0,0,0,-1,-1],[a,0]]
{ - [[n,n,0,0,0,-1,-1,0],[a]]
+ - [[n,n,0,0,0,-1,-1],[a]]
} - [[n,n,0,0,0,-1],[a,-1]]
} - [[n,n,0,0,0],[a,-1,-1]]
""""""" - [[n,n,0,0,0],[a,-1,-1]]
- top of Main is zero so we go straight
{ - [[n,n,0,0,0,-1],[a,-1]]
{ - [[n,n,0,0,0,-1,-1],[a]]
{ - [[n,n,0,0,0,-1,-1,a],]
Now this {
has three neighbouring instructions, so...
...if a
is zero, which it will be for perfect n
, then we go straight:
"!@ Main,Aux
" - [[n,n,0,0,0,-1,-1,a],]
- top of Main is a, which is zero, so we go straight
! - print top of Main, which is a, which is a 0
@ - exit the labyrinth
...if a
is non-zero, which it will be for non-perfect n
, then we turn left:
_~"!@ Main,Aux
_ - place a zero onto Main [[n,n,0,0,0,-1,-1,a,0],]
~ - bitwise NOT top of Main (=-1-x) [[n,n,0,0,0,-1,-1,a,-1],]
" - [[n,n,0,0,0,-1,-1,a,-1],]
- top of Main is NEGATIVE so we turn left
! - print top of Main, which is -1
@ - exit the labyrinth
$endgroup$
add a comment |
$begingroup$
Labyrinth, 80 bytes
?::`}:("(!@
perfect:
{:{:;%"}
+puts; "
}zero: "
}else{(:
"negI" _~
""""""{{{"!@
The Latin characters perfect puts zero else neg I
are actually just comments*.
i.e. if the input is perfect a 0
is printed, otherwise -1
is.
Try it online!
* so this or this work too...
?::`}:("(!@ ?::`}:("(!@
: BEWARE :
{:{:;%"} {:{:;%"}
+ ; " +LAIR; "
} : " } OF : "
} {(: }MINO{(:
" " _~ "TAUR" _~
""""""{{{"!@ """"""{{{"!@
How?
Takes as an input a positive integer n
and places an accumulator variable of -n
onto the auxiliary stack, then performs a divisibility test for each integer from n-1
down to, and including, 1
, adding any which do divide n
to the accumulator. Once this is complete if the accumulator variable is non-zero a -1
is output, otherwise a 0
is.
The ?::`}:(
is only executed once, at the beginning of execution:
?::`}:( Main,Aux
? - take an integer from STDIN and place it onto Main [[n],]
: - duplicate top of Main [[n,n],]
: - duplicate top of Main [[n,n,n],]
` - negate top of Main [[n,n,-n],]
} - place top of Main onto Aux [[n,n],[-n]]
: - duplicate top of Main [[n,n,n],[-n]]
( - decrement top of Main [[n,n,n-1],[-n]]
The next instruction, "
, is a no-op, but we have three neighbouring instructions so we branch according to the value at the top of Main, zero takes us forward, while non-zero takes us right.
If the input was 1
we go forward because the top of Main is zero:
(!@ Main,Aux
( - decrement top of Main [[1,1,-1],[-1]]
! - print top of Main, a -1
@ - exit the labyrinth
But if the input was greater than 1
we turn right because the top of Main is non-zero:
:} Main,Aux
: - duplicate top of Main [[n,n,n-1,n-1],[-n]]
} - place top of Main onto Aux [[n,n,n-1],[-n,n-1]]
At this point we have a three-neighbour branch, but we know n-1
is non-zero, so we turn right...
"% Main,Aux
" - no-op [[n,n,n-1],[-n,n-1]]
% - place modulo result onto Main [[n,n%(n-1)],[-n,n-1]]
- ...i.e we've got our first divisibility indicator n%(n-1), an
- accumulator, a=-n, and our potential divisor p=n-1:
- [[n,n%(n-1)],[a,p]]
We are now at another three-neighbour branch at %
.
If the result of %
was non-zero we go left to decrement our potential divisor, p=p-1
, and leave the accumulator, a
, as it is:
;:{(:""}" Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
- three-neighbour branch but n-1 is non-zero so we turn left
( - decrement top of Main [[n,n,p-1],[a]]
: - duplicate top of Main [[n,n,p-1,p-1],[a]]
"" - no-ops [[n,n,p-1,p-1],[a]]
} - place top of Main onto Aux [[n,n,p-1],[a,p-1]]
" - no-op [[n,n,p-1],[a,p-1]]
% - place modulo result onto Main [[n,n%(p-1)],[a,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if the result of %
was zero (for the first pass only when n=2
) we go straight on to BOTH add the divisor to our accumulator, a=a+p
, AND decrement our potential divisor, p=p-1
:
;:{:{+}}""""""""{(:""} Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
: - duplicate top of Main [[n,n,p,p],[a]]
{ - place top of Aux onto Main [[n,n,p,p,a],]
+ - perform addition [[n,n,p,a+p],]
} - place top of Main onto Aux [[n,n,p],[a+p]]
} - place top of Main onto Aux [[n,n],[a+p,p]]
""""""" - no-ops [[n,n],[a+p,p]]
- a branch, but n is non-zero so we turn left
" - no-op [[n,n],[a+p,p]]
{ - place top of Aux onto Main [[n,n,p],[a+p]]
- we branch, but p is non-zero so we turn right
( - decrement top of Main [[n,n,p-1],[a+p]]
: - duplicate top of Main [[n,n,p-1,p-1],[a+p]]
"" - no-ops [[n,n,p-1,p-1],[a+p]]
} - place top of Main onto Aux [[n,n,p-1],[a+p,p-1]]
At this point if p-1
is still non-zero we turn left:
"% Main,Aux
" - no-op [[n,n,p-1],[a+p,p-1]]
% - modulo [[n,n%(p-1)],[a+p,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if p-1
hit zero we go straight up to the :
on the second line of the labyrinth (you've seen all the instructions before, so I'm leaving their descriptions out and just giving their effect):
:":}"":({):""}"%;:{:{+}}"""""""{{{ Main,Aux
: - [[n,n,0,0],[a,0]]
" - [[n,n,0,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
: - [[n,n,0,0,0],[a,0]]
} - [[n,n,0,0],[a,0,0]]
- top of Main is zero so we go straight
"" - [[n,n,0,0],[a,0,0]]
: - [[n,n,0,0,0],[a,0,0]]
( - [[n,n,0,0,-1],[a,0,0]]
{ - [[n,n,0,0,-1,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
( - [[n,n,0,0,-1,-1],[a,0]]
: - [[n,n,0,0,-1,-1,-1],[a,0]]
"" - [[n,n,0,0,-1,-1,-1],[a,0]]
} - [[n,n,0,0,-1,-1],[a,0,-1]]
- top of Main is non-zero so we turn left
" - [[n,n,0,0,-1,-1],[a,0,-1]]
% - (-1)%(-1)=0 [[n,n,0,0,0],[a,0,-1]]
; - [[n,n,0,0],[a,0,-1]]
: - [[n,n,0,0,0],[a,0,-1]]
{ - [[n,n,0,0,0,-1],[a,0]]
: - [[n,n,0,0,0,-1,-1],[a,0]]
{ - [[n,n,0,0,0,-1,-1,0],[a]]
+ - [[n,n,0,0,0,-1,-1],[a]]
} - [[n,n,0,0,0,-1],[a,-1]]
} - [[n,n,0,0,0],[a,-1,-1]]
""""""" - [[n,n,0,0,0],[a,-1,-1]]
- top of Main is zero so we go straight
{ - [[n,n,0,0,0,-1],[a,-1]]
{ - [[n,n,0,0,0,-1,-1],[a]]
{ - [[n,n,0,0,0,-1,-1,a],]
Now this {
has three neighbouring instructions, so...
...if a
is zero, which it will be for perfect n
, then we go straight:
"!@ Main,Aux
" - [[n,n,0,0,0,-1,-1,a],]
- top of Main is a, which is zero, so we go straight
! - print top of Main, which is a, which is a 0
@ - exit the labyrinth
...if a
is non-zero, which it will be for non-perfect n
, then we turn left:
_~"!@ Main,Aux
_ - place a zero onto Main [[n,n,0,0,0,-1,-1,a,0],]
~ - bitwise NOT top of Main (=-1-x) [[n,n,0,0,0,-1,-1,a,-1],]
" - [[n,n,0,0,0,-1,-1,a,-1],]
- top of Main is NEGATIVE so we turn left
! - print top of Main, which is -1
@ - exit the labyrinth
$endgroup$
add a comment |
$begingroup$
Labyrinth, 80 bytes
?::`}:("(!@
perfect:
{:{:;%"}
+puts; "
}zero: "
}else{(:
"negI" _~
""""""{{{"!@
The Latin characters perfect puts zero else neg I
are actually just comments*.
i.e. if the input is perfect a 0
is printed, otherwise -1
is.
Try it online!
* so this or this work too...
?::`}:("(!@ ?::`}:("(!@
: BEWARE :
{:{:;%"} {:{:;%"}
+ ; " +LAIR; "
} : " } OF : "
} {(: }MINO{(:
" " _~ "TAUR" _~
""""""{{{"!@ """"""{{{"!@
How?
Takes as an input a positive integer n
and places an accumulator variable of -n
onto the auxiliary stack, then performs a divisibility test for each integer from n-1
down to, and including, 1
, adding any which do divide n
to the accumulator. Once this is complete if the accumulator variable is non-zero a -1
is output, otherwise a 0
is.
The ?::`}:(
is only executed once, at the beginning of execution:
?::`}:( Main,Aux
? - take an integer from STDIN and place it onto Main [[n],]
: - duplicate top of Main [[n,n],]
: - duplicate top of Main [[n,n,n],]
` - negate top of Main [[n,n,-n],]
} - place top of Main onto Aux [[n,n],[-n]]
: - duplicate top of Main [[n,n,n],[-n]]
( - decrement top of Main [[n,n,n-1],[-n]]
The next instruction, "
, is a no-op, but we have three neighbouring instructions so we branch according to the value at the top of Main, zero takes us forward, while non-zero takes us right.
If the input was 1
we go forward because the top of Main is zero:
(!@ Main,Aux
( - decrement top of Main [[1,1,-1],[-1]]
! - print top of Main, a -1
@ - exit the labyrinth
But if the input was greater than 1
we turn right because the top of Main is non-zero:
:} Main,Aux
: - duplicate top of Main [[n,n,n-1,n-1],[-n]]
} - place top of Main onto Aux [[n,n,n-1],[-n,n-1]]
At this point we have a three-neighbour branch, but we know n-1
is non-zero, so we turn right...
"% Main,Aux
" - no-op [[n,n,n-1],[-n,n-1]]
% - place modulo result onto Main [[n,n%(n-1)],[-n,n-1]]
- ...i.e we've got our first divisibility indicator n%(n-1), an
- accumulator, a=-n, and our potential divisor p=n-1:
- [[n,n%(n-1)],[a,p]]
We are now at another three-neighbour branch at %
.
If the result of %
was non-zero we go left to decrement our potential divisor, p=p-1
, and leave the accumulator, a
, as it is:
;:{(:""}" Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
- three-neighbour branch but n-1 is non-zero so we turn left
( - decrement top of Main [[n,n,p-1],[a]]
: - duplicate top of Main [[n,n,p-1,p-1],[a]]
"" - no-ops [[n,n,p-1,p-1],[a]]
} - place top of Main onto Aux [[n,n,p-1],[a,p-1]]
" - no-op [[n,n,p-1],[a,p-1]]
% - place modulo result onto Main [[n,n%(p-1)],[a,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if the result of %
was zero (for the first pass only when n=2
) we go straight on to BOTH add the divisor to our accumulator, a=a+p
, AND decrement our potential divisor, p=p-1
:
;:{:{+}}""""""""{(:""} Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
: - duplicate top of Main [[n,n,p,p],[a]]
{ - place top of Aux onto Main [[n,n,p,p,a],]
+ - perform addition [[n,n,p,a+p],]
} - place top of Main onto Aux [[n,n,p],[a+p]]
} - place top of Main onto Aux [[n,n],[a+p,p]]
""""""" - no-ops [[n,n],[a+p,p]]
- a branch, but n is non-zero so we turn left
" - no-op [[n,n],[a+p,p]]
{ - place top of Aux onto Main [[n,n,p],[a+p]]
- we branch, but p is non-zero so we turn right
( - decrement top of Main [[n,n,p-1],[a+p]]
: - duplicate top of Main [[n,n,p-1,p-1],[a+p]]
"" - no-ops [[n,n,p-1,p-1],[a+p]]
} - place top of Main onto Aux [[n,n,p-1],[a+p,p-1]]
At this point if p-1
is still non-zero we turn left:
"% Main,Aux
" - no-op [[n,n,p-1],[a+p,p-1]]
% - modulo [[n,n%(p-1)],[a+p,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if p-1
hit zero we go straight up to the :
on the second line of the labyrinth (you've seen all the instructions before, so I'm leaving their descriptions out and just giving their effect):
:":}"":({):""}"%;:{:{+}}"""""""{{{ Main,Aux
: - [[n,n,0,0],[a,0]]
" - [[n,n,0,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
: - [[n,n,0,0,0],[a,0]]
} - [[n,n,0,0],[a,0,0]]
- top of Main is zero so we go straight
"" - [[n,n,0,0],[a,0,0]]
: - [[n,n,0,0,0],[a,0,0]]
( - [[n,n,0,0,-1],[a,0,0]]
{ - [[n,n,0,0,-1,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
( - [[n,n,0,0,-1,-1],[a,0]]
: - [[n,n,0,0,-1,-1,-1],[a,0]]
"" - [[n,n,0,0,-1,-1,-1],[a,0]]
} - [[n,n,0,0,-1,-1],[a,0,-1]]
- top of Main is non-zero so we turn left
" - [[n,n,0,0,-1,-1],[a,0,-1]]
% - (-1)%(-1)=0 [[n,n,0,0,0],[a,0,-1]]
; - [[n,n,0,0],[a,0,-1]]
: - [[n,n,0,0,0],[a,0,-1]]
{ - [[n,n,0,0,0,-1],[a,0]]
: - [[n,n,0,0,0,-1,-1],[a,0]]
{ - [[n,n,0,0,0,-1,-1,0],[a]]
+ - [[n,n,0,0,0,-1,-1],[a]]
} - [[n,n,0,0,0,-1],[a,-1]]
} - [[n,n,0,0,0],[a,-1,-1]]
""""""" - [[n,n,0,0,0],[a,-1,-1]]
- top of Main is zero so we go straight
{ - [[n,n,0,0,0,-1],[a,-1]]
{ - [[n,n,0,0,0,-1,-1],[a]]
{ - [[n,n,0,0,0,-1,-1,a],]
Now this {
has three neighbouring instructions, so...
...if a
is zero, which it will be for perfect n
, then we go straight:
"!@ Main,Aux
" - [[n,n,0,0,0,-1,-1,a],]
- top of Main is a, which is zero, so we go straight
! - print top of Main, which is a, which is a 0
@ - exit the labyrinth
...if a
is non-zero, which it will be for non-perfect n
, then we turn left:
_~"!@ Main,Aux
_ - place a zero onto Main [[n,n,0,0,0,-1,-1,a,0],]
~ - bitwise NOT top of Main (=-1-x) [[n,n,0,0,0,-1,-1,a,-1],]
" - [[n,n,0,0,0,-1,-1,a,-1],]
- top of Main is NEGATIVE so we turn left
! - print top of Main, which is -1
@ - exit the labyrinth
$endgroup$
Labyrinth, 80 bytes
?::`}:("(!@
perfect:
{:{:;%"}
+puts; "
}zero: "
}else{(:
"negI" _~
""""""{{{"!@
The Latin characters perfect puts zero else neg I
are actually just comments*.
i.e. if the input is perfect a 0
is printed, otherwise -1
is.
Try it online!
* so this or this work too...
?::`}:("(!@ ?::`}:("(!@
: BEWARE :
{:{:;%"} {:{:;%"}
+ ; " +LAIR; "
} : " } OF : "
} {(: }MINO{(:
" " _~ "TAUR" _~
""""""{{{"!@ """"""{{{"!@
How?
Takes as an input a positive integer n
and places an accumulator variable of -n
onto the auxiliary stack, then performs a divisibility test for each integer from n-1
down to, and including, 1
, adding any which do divide n
to the accumulator. Once this is complete if the accumulator variable is non-zero a -1
is output, otherwise a 0
is.
The ?::`}:(
is only executed once, at the beginning of execution:
?::`}:( Main,Aux
? - take an integer from STDIN and place it onto Main [[n],]
: - duplicate top of Main [[n,n],]
: - duplicate top of Main [[n,n,n],]
` - negate top of Main [[n,n,-n],]
} - place top of Main onto Aux [[n,n],[-n]]
: - duplicate top of Main [[n,n,n],[-n]]
( - decrement top of Main [[n,n,n-1],[-n]]
The next instruction, "
, is a no-op, but we have three neighbouring instructions so we branch according to the value at the top of Main, zero takes us forward, while non-zero takes us right.
If the input was 1
we go forward because the top of Main is zero:
(!@ Main,Aux
( - decrement top of Main [[1,1,-1],[-1]]
! - print top of Main, a -1
@ - exit the labyrinth
But if the input was greater than 1
we turn right because the top of Main is non-zero:
:} Main,Aux
: - duplicate top of Main [[n,n,n-1,n-1],[-n]]
} - place top of Main onto Aux [[n,n,n-1],[-n,n-1]]
At this point we have a three-neighbour branch, but we know n-1
is non-zero, so we turn right...
"% Main,Aux
" - no-op [[n,n,n-1],[-n,n-1]]
% - place modulo result onto Main [[n,n%(n-1)],[-n,n-1]]
- ...i.e we've got our first divisibility indicator n%(n-1), an
- accumulator, a=-n, and our potential divisor p=n-1:
- [[n,n%(n-1)],[a,p]]
We are now at another three-neighbour branch at %
.
If the result of %
was non-zero we go left to decrement our potential divisor, p=p-1
, and leave the accumulator, a
, as it is:
;:{(:""}" Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
- three-neighbour branch but n-1 is non-zero so we turn left
( - decrement top of Main [[n,n,p-1],[a]]
: - duplicate top of Main [[n,n,p-1,p-1],[a]]
"" - no-ops [[n,n,p-1,p-1],[a]]
} - place top of Main onto Aux [[n,n,p-1],[a,p-1]]
" - no-op [[n,n,p-1],[a,p-1]]
% - place modulo result onto Main [[n,n%(p-1)],[a,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if the result of %
was zero (for the first pass only when n=2
) we go straight on to BOTH add the divisor to our accumulator, a=a+p
, AND decrement our potential divisor, p=p-1
:
;:{:{+}}""""""""{(:""} Main,Aux
; - drop top of Main [[n],[a,p]]
: - duplicate top of Main [[n,n],[a,p]]
{ - place top of Aux onto Main [[n,n,p],[a]]
: - duplicate top of Main [[n,n,p,p],[a]]
{ - place top of Aux onto Main [[n,n,p,p,a],]
+ - perform addition [[n,n,p,a+p],]
} - place top of Main onto Aux [[n,n,p],[a+p]]
} - place top of Main onto Aux [[n,n],[a+p,p]]
""""""" - no-ops [[n,n],[a+p,p]]
- a branch, but n is non-zero so we turn left
" - no-op [[n,n],[a+p,p]]
{ - place top of Aux onto Main [[n,n,p],[a+p]]
- we branch, but p is non-zero so we turn right
( - decrement top of Main [[n,n,p-1],[a+p]]
: - duplicate top of Main [[n,n,p-1,p-1],[a+p]]
"" - no-ops [[n,n,p-1,p-1],[a+p]]
} - place top of Main onto Aux [[n,n,p-1],[a+p,p-1]]
At this point if p-1
is still non-zero we turn left:
"% Main,Aux
" - no-op [[n,n,p-1],[a+p,p-1]]
% - modulo [[n,n%(p-1)],[a+p,p-1]]
- ...and we branch again according to the divisibility
- of n by our new potential divisor, p-1
...but if p-1
hit zero we go straight up to the :
on the second line of the labyrinth (you've seen all the instructions before, so I'm leaving their descriptions out and just giving their effect):
:":}"":({):""}"%;:{:{+}}"""""""{{{ Main,Aux
: - [[n,n,0,0],[a,0]]
" - [[n,n,0,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
: - [[n,n,0,0,0],[a,0]]
} - [[n,n,0,0],[a,0,0]]
- top of Main is zero so we go straight
"" - [[n,n,0,0],[a,0,0]]
: - [[n,n,0,0,0],[a,0,0]]
( - [[n,n,0,0,-1],[a,0,0]]
{ - [[n,n,0,0,-1,0],[a,0]]
- top of Main is zero so we go straight
- ...but we hit the wall and so turn around
( - [[n,n,0,0,-1,-1],[a,0]]
: - [[n,n,0,0,-1,-1,-1],[a,0]]
"" - [[n,n,0,0,-1,-1,-1],[a,0]]
} - [[n,n,0,0,-1,-1],[a,0,-1]]
- top of Main is non-zero so we turn left
" - [[n,n,0,0,-1,-1],[a,0,-1]]
% - (-1)%(-1)=0 [[n,n,0,0,0],[a,0,-1]]
; - [[n,n,0,0],[a,0,-1]]
: - [[n,n,0,0,0],[a,0,-1]]
{ - [[n,n,0,0,0,-1],[a,0]]
: - [[n,n,0,0,0,-1,-1],[a,0]]
{ - [[n,n,0,0,0,-1,-1,0],[a]]
+ - [[n,n,0,0,0,-1,-1],[a]]
} - [[n,n,0,0,0,-1],[a,-1]]
} - [[n,n,0,0,0],[a,-1,-1]]
""""""" - [[n,n,0,0,0],[a,-1,-1]]
- top of Main is zero so we go straight
{ - [[n,n,0,0,0,-1],[a,-1]]
{ - [[n,n,0,0,0,-1,-1],[a]]
{ - [[n,n,0,0,0,-1,-1,a],]
Now this {
has three neighbouring instructions, so...
...if a
is zero, which it will be for perfect n
, then we go straight:
"!@ Main,Aux
" - [[n,n,0,0,0,-1,-1,a],]
- top of Main is a, which is zero, so we go straight
! - print top of Main, which is a, which is a 0
@ - exit the labyrinth
...if a
is non-zero, which it will be for non-perfect n
, then we turn left:
_~"!@ Main,Aux
_ - place a zero onto Main [[n,n,0,0,0,-1,-1,a,0],]
~ - bitwise NOT top of Main (=-1-x) [[n,n,0,0,0,-1,-1,a,-1],]
" - [[n,n,0,0,0,-1,-1,a,-1],]
- top of Main is NEGATIVE so we turn left
! - print top of Main, which is -1
@ - exit the labyrinth
edited Mar 16 at 21:17
answered Mar 15 at 23:09
Jonathan AllanJonathan Allan
54.4k537174
54.4k537174
add a comment |
add a comment |
$begingroup$
Batch, 81 bytes
@set s=-%1
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@if %s%==%1 echo 1
Takes n
as a command-line parameter and outputs 1
if it is a perfect number. Brute force method, starts the sum at -n
so that it can include n
itself in the loop.
$endgroup$
add a comment |
$begingroup$
Batch, 81 bytes
@set s=-%1
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@if %s%==%1 echo 1
Takes n
as a command-line parameter and outputs 1
if it is a perfect number. Brute force method, starts the sum at -n
so that it can include n
itself in the loop.
$endgroup$
add a comment |
$begingroup$
Batch, 81 bytes
@set s=-%1
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@if %s%==%1 echo 1
Takes n
as a command-line parameter and outputs 1
if it is a perfect number. Brute force method, starts the sum at -n
so that it can include n
itself in the loop.
$endgroup$
Batch, 81 bytes
@set s=-%1
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@if %s%==%1 echo 1
Takes n
as a command-line parameter and outputs 1
if it is a perfect number. Brute force method, starts the sum at -n
so that it can include n
itself in the loop.
answered Mar 12 at 9:40
NeilNeil
82.8k745179
82.8k745179
add a comment |
add a comment |
$begingroup$
Charcoal, 13 bytes
Nθ⁼θΣΦθ∧ι¬﹪θι
Try it online! Link is to verbose version of code. Outputs -
for perfect numbers. Uses brute force. Explanation:
Nθ Numeric input
Φθ Filter on implicit range
ι Current value (is non-zero)
∧ Logical And
θ Input value
﹪ Modulo
ι Current value
¬ Is zero
Σ Sum of matching values
⁼ Equals
θ Input value
$endgroup$
add a comment |
$begingroup$
Charcoal, 13 bytes
Nθ⁼θΣΦθ∧ι¬﹪θι
Try it online! Link is to verbose version of code. Outputs -
for perfect numbers. Uses brute force. Explanation:
Nθ Numeric input
Φθ Filter on implicit range
ι Current value (is non-zero)
∧ Logical And
θ Input value
﹪ Modulo
ι Current value
¬ Is zero
Σ Sum of matching values
⁼ Equals
θ Input value
$endgroup$
add a comment |
$begingroup$
Charcoal, 13 bytes
Nθ⁼θΣΦθ∧ι¬﹪θι
Try it online! Link is to verbose version of code. Outputs -
for perfect numbers. Uses brute force. Explanation:
Nθ Numeric input
Φθ Filter on implicit range
ι Current value (is non-zero)
∧ Logical And
θ Input value
﹪ Modulo
ι Current value
¬ Is zero
Σ Sum of matching values
⁼ Equals
θ Input value
$endgroup$
Charcoal, 13 bytes
Nθ⁼θΣΦθ∧ι¬﹪θι
Try it online! Link is to verbose version of code. Outputs -
for perfect numbers. Uses brute force. Explanation:
Nθ Numeric input
Φθ Filter on implicit range
ι Current value (is non-zero)
∧ Logical And
θ Input value
﹪ Modulo
ι Current value
¬ Is zero
Σ Sum of matching values
⁼ Equals
θ Input value
answered Mar 12 at 10:06
NeilNeil
82.8k745179
82.8k745179
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 14 bytes
PerfectNumberQ
Try it online!
$endgroup$
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
add a comment |
$begingroup$
Wolfram Language (Mathematica), 14 bytes
PerfectNumberQ
Try it online!
$endgroup$
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
add a comment |
$begingroup$
Wolfram Language (Mathematica), 14 bytes
PerfectNumberQ
Try it online!
$endgroup$
Wolfram Language (Mathematica), 14 bytes
PerfectNumberQ
Try it online!
answered Mar 12 at 10:19
J42161217J42161217
14k21353
14k21353
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
add a comment |
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
$begingroup$
Yes, Mathematica! Another built-in.
$endgroup$
– tsh
Mar 12 at 10:21
add a comment |
$begingroup$
Pyth, 8 bytes
qs{*MPyP
Try it online here.
qs{*MPyPQQ Implicit: Q=eval(input())
Trailing QQ inferred
PQ Prime factors of Q
y Powerset
P Remove last element - this will always be the full prime factorisation
*M Take product of each
{ Deduplicate
s Sum
q Q Is the above equal to Q? Implicit print
$endgroup$
add a comment |
$begingroup$
Pyth, 8 bytes
qs{*MPyP
Try it online here.
qs{*MPyPQQ Implicit: Q=eval(input())
Trailing QQ inferred
PQ Prime factors of Q
y Powerset
P Remove last element - this will always be the full prime factorisation
*M Take product of each
{ Deduplicate
s Sum
q Q Is the above equal to Q? Implicit print
$endgroup$
add a comment |
$begingroup$
Pyth, 8 bytes
qs{*MPyP
Try it online here.
qs{*MPyPQQ Implicit: Q=eval(input())
Trailing QQ inferred
PQ Prime factors of Q
y Powerset
P Remove last element - this will always be the full prime factorisation
*M Take product of each
{ Deduplicate
s Sum
q Q Is the above equal to Q? Implicit print
$endgroup$
Pyth, 8 bytes
qs{*MPyP
Try it online here.
qs{*MPyPQQ Implicit: Q=eval(input())
Trailing QQ inferred
PQ Prime factors of Q
y Powerset
P Remove last element - this will always be the full prime factorisation
*M Take product of each
{ Deduplicate
s Sum
q Q Is the above equal to Q? Implicit print
answered Mar 12 at 10:28
SokSok
4,197925
4,197925
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 44 bytes
.+
$*
M!&`(.+)$(?<=^1+)
+`^1(1*¶+)1
$1
^¶+$
Try it online! Uses brute force, so link only includes the faster test cases. Explanation:
.+
$*
Convert to unary.
M!&`(.+)$(?<=^1+)
Match all factors of the input. This uses overlapping mode, which in Retina 0.8.2 requires all of the matches to start at different positions, so the matches are actually returned in descending order, starting with the original input.
+`^1(1*¶+)1
$1
Subtract the proper factors from the input.
^¶+$
Test whether the result is zero.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 44 bytes
.+
$*
M!&`(.+)$(?<=^1+)
+`^1(1*¶+)1
$1
^¶+$
Try it online! Uses brute force, so link only includes the faster test cases. Explanation:
.+
$*
Convert to unary.
M!&`(.+)$(?<=^1+)
Match all factors of the input. This uses overlapping mode, which in Retina 0.8.2 requires all of the matches to start at different positions, so the matches are actually returned in descending order, starting with the original input.
+`^1(1*¶+)1
$1
Subtract the proper factors from the input.
^¶+$
Test whether the result is zero.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 44 bytes
.+
$*
M!&`(.+)$(?<=^1+)
+`^1(1*¶+)1
$1
^¶+$
Try it online! Uses brute force, so link only includes the faster test cases. Explanation:
.+
$*
Convert to unary.
M!&`(.+)$(?<=^1+)
Match all factors of the input. This uses overlapping mode, which in Retina 0.8.2 requires all of the matches to start at different positions, so the matches are actually returned in descending order, starting with the original input.
+`^1(1*¶+)1
$1
Subtract the proper factors from the input.
^¶+$
Test whether the result is zero.
$endgroup$
Retina 0.8.2, 44 bytes
.+
$*
M!&`(.+)$(?<=^1+)
+`^1(1*¶+)1
$1
^¶+$
Try it online! Uses brute force, so link only includes the faster test cases. Explanation:
.+
$*
Convert to unary.
M!&`(.+)$(?<=^1+)
Match all factors of the input. This uses overlapping mode, which in Retina 0.8.2 requires all of the matches to start at different positions, so the matches are actually returned in descending order, starting with the original input.
+`^1(1*¶+)1
$1
Subtract the proper factors from the input.
^¶+$
Test whether the result is zero.
answered Mar 12 at 10:33
NeilNeil
82.8k745179
82.8k745179
add a comment |
add a comment |
$begingroup$
Java 8, 66 bytes
Someone has to use the stream API at some point, even if there's a shorter way to do it
n->java.util.stream.IntStream.range(1,n).filter(i->n%i<1).sum()==n
Try it online!
$endgroup$
add a comment |
$begingroup$
Java 8, 66 bytes
Someone has to use the stream API at some point, even if there's a shorter way to do it
n->java.util.stream.IntStream.range(1,n).filter(i->n%i<1).sum()==n
Try it online!
$endgroup$
add a comment |
$begingroup$
Java 8, 66 bytes
Someone has to use the stream API at some point, even if there's a shorter way to do it
n->java.util.stream.IntStream.range(1,n).filter(i->n%i<1).sum()==n
Try it online!
$endgroup$
Java 8, 66 bytes
Someone has to use the stream API at some point, even if there's a shorter way to do it
n->java.util.stream.IntStream.range(1,n).filter(i->n%i<1).sum()==n
Try it online!
answered Mar 12 at 13:11
Benjamin UrquhartBenjamin Urquhart
52018
52018
add a comment |
add a comment |
$begingroup$
cQuents, 8 bytes
?#N=UzN
Try it online!
Explanation
? Mode query: return whether or not input is in sequence
# Conditional: iterate N, add N to sequence if condition is true
N= Condition: N ==
U ) sum( )
z ) proper_divisors( )
N N
)) implicit
$endgroup$
add a comment |
$begingroup$
cQuents, 8 bytes
?#N=UzN
Try it online!
Explanation
? Mode query: return whether or not input is in sequence
# Conditional: iterate N, add N to sequence if condition is true
N= Condition: N ==
U ) sum( )
z ) proper_divisors( )
N N
)) implicit
$endgroup$
add a comment |
$begingroup$
cQuents, 8 bytes
?#N=UzN
Try it online!
Explanation
? Mode query: return whether or not input is in sequence
# Conditional: iterate N, add N to sequence if condition is true
N= Condition: N ==
U ) sum( )
z ) proper_divisors( )
N N
)) implicit
$endgroup$
cQuents, 8 bytes
?#N=UzN
Try it online!
Explanation
? Mode query: return whether or not input is in sequence
# Conditional: iterate N, add N to sequence if condition is true
N= Condition: N ==
U ) sum( )
z ) proper_divisors( )
N N
)) implicit
answered Mar 12 at 15:36
StephenStephen
7,54623397
7,54623397
add a comment |
add a comment |
1 2
next
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181350%2fam-i-not-good-enough-for-you%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Wait, so truthy is for values that aren't perfect, and falsey is for values that are?
$endgroup$
– Esolanging Fruit
Mar 12 at 2:57
2
$begingroup$
@Tvde1 Proper divisors have to less than the number, otherwise no number other than
1
would be perfect, since every number is divisible by1
and itself. The sum of proper divisors of1
is0
$endgroup$
– Jo King
Mar 12 at 7:40
3
$begingroup$
@Grimy Only if you can prove so. Good luck! (though I'm wondering how that would save bytes)
$endgroup$
– Jo King
Mar 12 at 9:15
1
$begingroup$
So no, too bad. It would cut the size of an ECMA regex answer by a factor of about 3.
$endgroup$
– Grimy
Mar 12 at 9:18
3
$begingroup$
"Output can be two distinct and consistent values" - may we not use "truthy vs falsey" here (e.g. for Python using zero vs non zero; a list with content vs an empty list; and combinations thereof)?
$endgroup$
– Jonathan Allan
Mar 12 at 10:15