Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 761 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 15 bytes

`@Z\s@E=vtsG<}n

Very slow. It keeps trying increasing numbers one by one until the n-th perfect number is found.

Try it online!

Explanation

` % Do...while
 @ % Push iteration index, k (starting at 1)
 Z\ % Array of divisors
 s % Sum
 @E % Push k. Multiply by 2
 = % Equal? If so, k is a perfect number
 v % Concatenate vertically. This gradually builds an array which at the k-th
 % iteration contains k zero/one values, where ones indicate perfect numbers
 ts % Duplicate. Sum of array
 G< % Push input. Less than? This is the loop condition: if true, proceed with
 % next iteration
} % Finally (execute right before exiting loop)
 n % Number of elements of the array
 % End (implicit). Display (implicit)

MATL, 15 bytes

`@Z\s@E=vtsG<}n

Very slow. It keeps trying increasing numbers one by one until the n-th perfect number is found.

Try it online!

MATL, 15 bytes

`@Z\s@E=vtsG<}n

Very slow. It keeps trying increasing numbers one by one until the n-th perfect number is found.

Try it online!

Explanation

` % Do...while
 @ % Push iteration index, k (starting at 1)
 Z\ % Array of divisors
 s % Sum
 @E % Push k. Multiply by 2
 = % Equal? If so, k is a perfect number
 v % Concatenate vertically. This gradually builds an array which at the k-th
 % iteration contains k zero/one values, where ones indicate perfect numbers
 ts % Duplicate. Sum of array
 G< % Push input. Less than? This is the loop condition: if true, proceed with
 % next iteration
} % Finally (execute right before exiting loop)
 n % Number of elements of the array
 % End (implicit). Display (implicit)
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 15 bytes

`@Z\s@E=vtsG<}n

Very slow. It keeps trying increasing numbers one by one until the n-th perfect number is found.

Try it online!

AltStyle によって変換されたページ (->オリジナル) /