Number sum medley

2

0

Winner

CJam, 74 bytes by Optimizer

Task

  • Take an input of an integer n through the console or a file.
  • Find the sum of the nth prime, square, triangular number, and cube. Add to that the factorial of the first decimal place of the square root of n and the xth Fibonacci number, where x is the first digit of the square of n.
  • Print that sum to the console or to a file.

Rules

  • Entries must complete the task above for all positive integers less than 100.
  • Standard loopholes are of course not allowed.
  • Any built-in function that finds primes or tests for primes in any way is not allowed.
  • This is code golf.

Summary of numbers to sum

nth prime (where 2 is the first)
square of n
nth triangular number (where 1 is the first)
cube of n
factorial of the first decimal place of the square root of n
xth Fibonacci number, where x is the first digit of the square of n (2 is the third Fibonacci number)

Examples

Note: The numbers being summed are included here just for clarity.

Input: 1
Output: 2 + 1 + 1 + 1 + 1 + 1 = 7

Input: 11
Output: 31 + 121 + 66 + 1331 + 6 + 1 = 1556

Input: 99
Output: 523 + 9801 + 4950 + 970299 + 362880 + 34 = 1348487

monopole

Posted 2014-10-01T20:39:38.667

Reputation: 1 559

Answers

2

CJam, 83 76 75 74 bytes

This can be golfed more. Will update when I golf it further.

T1{_2$+}A*]ri:NN*s1<i={X):Xmf,1=T+:TN<}gXNN*_N*N_)*2/Nmqs_'.#)=~,:)1+:*]:+

The online compiler for CJam does not return proper Double numbers(required by mq, root). So try it in the Java version as mentioned on this page

Optimizer

Posted 2014-10-01T20:39:38.667

Reputation: 25 836

3

Java, 323

class C {public static void main(String[]s){int n=new java.util.Scanner(System.in).nextInt(),p=2,j,k=1,t=0,a=1,b=0,c;z:for(;t<n;p++){for(j=1;++j<p;)if(p%j<1)continue z;t++;}j=(int)Math.round(Math.sqrt(n)%1*10);for(t=0;t++<j;k*=t);for(c=j=n*n;(j/=10)>9;);for(t=0;++t<j;a+=b,b=a-b);System.out.print(p-1+c+(c+n)/2+c*n+k+a);}}

Ungolfed:

class C {
    public static void main(String[] args) {
        int n = new java.util.Scanner(System.in).nextInt(),                                                                    p = 2,
            j,
            k = 1,
            t,
            a = 1,
            b = 0,
            c;

        // prime
        z:
        for (t = 0; t < n; p++) {
            for (j = 1; ++j < p; ) {
                if (p % j < 1) {
                    continue z;
                }
            }
            t++;
        }

        // factorial
        j = (int) Math.round(Math.sqrt(n) % 1 * 10);
        for (t = 0; t++ < j; k *= t);

        // fibonacci
        for (c = j = n * n; (j /= 10) > 9;);
        for (t = 0; ++t < j; ) {
             a += b, b = a - b);
        }

        System.out.print(p - 1 + c + (c + n) / 2 + c * n + k + a);
    }
}

Ypnypn

Posted 2014-10-01T20:39:38.667

Reputation: 10 485

I think a += b, b = a - b should not have a ) before the semicolon. – me and my cat – 2014-10-03T15:50:50.197

2

Mathematica, 152 136 133 132 bytes

For[n=Input[i=m=0],m<n,If[Divisors@++i=={1,i},m++]];#&@@Fibonacci@IntegerDigits[n^2]+i+3n^2/2+n/2+n^3+Floor[-10(Floor[s=Sqrt@n]-s)]!

I'm first determining the nth prime number in a loop (also avoiding NextPrime - it's not explicitly disallowed but I think it's not intended to be allowed either), and then I'm simply summing all the required numbers in a single expression. There is one simplification: I'm combining n2 and the triangular number n(n+1)/2 into 3n2/2 and n/2.

Input is taking via prompt, which is the closest thing to STDIN you get in Mathematica.

Martin Ender

Posted 2014-10-01T20:39:38.667

Reputation: 184 808

0

JavaScript (Node.js), 159 bytes

n=>{
f=a=b=1
for(N=n**.5*10%10|0;N;)
f*=N--
for(N=(n*n+"")[0];--N;)
b=a+(a=b)
f+=a+n*(n*n+n+-~n/2)
for(i=2;n;n-=j<=i++)
for(j=1;++j<i;)
j=i%j?j:i
return f+i-1}

Try it online!

DanielIndie

Posted 2014-10-01T20:39:38.667

Reputation: 1 220