จำนวนวิธีที่ตัวเลขคือผลรวมของช่วงเวลาที่ต่อเนื่องกัน


15

กำหนดจำนวนเต็มมากกว่า 1 ส่งออกจำนวนวิธีที่มันสามารถแสดงเป็นผลรวมของช่วงเวลาหนึ่งหรือมากกว่าติดต่อกัน

ลำดับของการสรุปไม่สำคัญ ผลรวมสามารถประกอบด้วยตัวเลขเดียว (ดังนั้นผลลัพธ์สำหรับนายกใด ๆ จะมีอย่างน้อย 1)

นี่คือรหัสกอล์ฟใช้กฎมาตรฐาน

ดูนี้วิกิพีเดีย OEISสำหรับข้อมูลที่เกี่ยวข้องและลำดับรวมทั้งลำดับของตัวเองOEIS A054845

กรณีทดสอบ

2 => 1
3 => 1
4 => 0
5 => 2
6 => 0
7 => 1
8 => 1
10 => 1
36 => 2
41 => 3
42 => 1
43 => 1
44 => 0
311 => 5
1151 => 4
34421 => 6

คำตอบ:


9

เยลลี่ ,  6  5 ไบต์

-1 ขอบคุณดิลิน

ÆRẆ§ċ

ลิงก์ monadic

ลองออนไลน์! หรือดูชุดทดสอบ (หมายเหตุกรณีทดสอบสุดท้ายจะหมดเวลาที่ 60s ที่ TIO)

อย่างไร?

ÆRẆ§ċ - Link: integer, n
ÆR    - primes from 2 to n inclusive
  Ẇ   - all contiguous substrings
   §  - sum each
    ċ - count occurrences of n

2æRเหมือนกับÆR
dylnan

@dylnan ดีคนหนึ่งขอบคุณ!
Jonathan Allan

8

R , 95 ไบต์

function(x,P=2){for(i in 2:x)P=c(P,i[all(i%%P)])
for(i in 1:x){F=F+sum(cumsum(P)==x)
P[i]=0}
F}

ลองออนไลน์!

  • -24 ไบต์ขอบคุณ @Giuseppe ที่ปฏิวัติการแก้ปัญหาของฉันรองรับ 34421 ด้วยเช่นกัน!

1
นั่นเป็นวิธีที่ชาญฉลาดในการคิดค่าใช้จ่ายจนถึงx!
Giuseppe


1
@Giuseppe: เยี่ยมมาก !! วันนี้ฉันป่วยและฉันจะไม่สามารถคิดว่า ... (อาจจะไม่เคย: P) ฉันรู้สึกแย่ในการใช้รหัสของคุณ ... ฉันย้อนกลับไปก่อนหน้าถ้าคุณโพสต์คำตอบใหม่ฉัน ll upvote;)
digEmAll

1
@ngm ความสำคัญของ 34421 .. คืออะไร? และ @digEmAll ฉันไม่สนใจหรอก ฉันไม่มีข้อสงสัยเกี่ยวกับการใช้งานcumsumและการตั้งค่าองค์ประกอบสองสามอย่างแรกเพื่อ0ให้ได้ผลรวมที่ดีที่สุด ไพร์มกอล์ฟเป็นเพียงฉันที่พยายามทำให้เคสทดสอบสุดท้ายทำงานและฉันแค่โชคดีที่มันสั้นกว่าouter! ฉันมีตัวแทนมากเกินพอ (อย่างน้อยก็จนกว่าเราจะได้รับข้อกำหนดด้านตัวแทนที่เหมาะสม) และฉันก็ยินดีที่จะช่วยนักกอล์ฟ R ให้มองเห็นได้มากขึ้น!
Giuseppe

1
@Giuseppe 34421 เป็นจำนวนที่เล็กที่สุดซึ่งเป็นผลรวมของช่วงเวลาต่อเนื่องใน 6 วิธี (ดูoeis.org/A054859 ) โซลูชันส่วนใหญ่ที่โพสต์สำหรับความท้าทายนี้หมดเวลา (ใน TIO) หรือหน่วยความจำสำหรับกรณีทดสอบนั้น แม้ว่าคำตอบของจาวาจะได้จำนวนเต็มที่ต่อไปในลำดับด้วย (สำหรับ 7) แต่ไม่ใช่สำหรับ 8
ngm


4

JavaScript (ES6), 92 ไบต์

n=>(a=[],k=1,g=s=>k>n?0:!s+g(s>0?s-(p=d=>k%--d?p(d):d<2&&a.push(k)&&k)(++k):s+a.shift()))(n)

ลองออนไลน์!

แสดงความคิดเห็น

n => (                          // n = input
  a = [],                       // a[] = array holding the list of consecutive primes
  k = 1,                        // k = current number to test
  g = s =>                      // g = recursive function taking s = n - sum(a)
    k > n ?                     //   if k is greater than n:
      0                         //     stop recursion
    :                           //   else:
      !s +                      //     increment the final result if s = 0
      g(                        //     add the result of a recursive call to g():
        s > 0 ?                 //       if s is positive:
          s - (                 //         subtract from s the result of p():
            p = d => k % --d ?  //           p() = recursive helper function looking
              p(d)              //                 for the highest divisor d of k,
            :                   //                 starting with d = k - 1
              d < 2 &&          //           if d is less than 2 (i.e. k is prime):
              a.push(k) &&      //             append k to a[]
              k                 //             and return k (else: return false)
          )(++k)                //         increment k and call p(k)
        :                       //       else:
          s + a.shift()         //         remove the first entry from a[]
                                //         and add it to s
      )                         //     end of recursive call
  )(n)                          // initial call to g() with s = n

4

MATL, 15 12 ไบต์

EZqPYTRYsG=z

ลองใช้กับ MATL Online

ค่าเริ่มต้นE(คูณด้วย 2) ทำให้แน่ใจว่าสำหรับอินพุตหลักผลลัพธ์ของภายหลังYs( cumsum) ไม่มีอินพุตไพรม์ซ้ำตัวเองในส่วนที่เป็นศูนย์ของเมทริกซ์ (ดังนั้นจะยุ่งกับการนับ)

คำอธิบาย:

                % Implicit input, say 5
E               % Double the input
 Zq             % Get list of primes upto (and including) that
                %  Stack: [2 3 5 7]
   P            % Reverse that list
    YT          % Toeplitz matrix of that
                %  Stack: [7 5 3 2
                           5 7 5 3
                           3 5 7 5
                           2 3 5 7]
      R         % `triu` - upper triangular portion of matrix
                %  Stack: [7 5 3 2
                           0 7 5 3
                           0 0 7 5
                           0 0 0 7]
       Ys       % Cumulative sum along each column
                %  Stack: [7  5  3  2
                           7 12  8  5
                           7 12 15 10
                           7 12 15 17]


         G=     % Compare against input - 1s where equal, 0s where not
           z    % Count the number of non-zeros

1
เมทริกซ์ Toeplitz ของจำนวนเฉพาะและส่วนสามเหลี่ยมดีมาก!
Luis Mendo

4

Brachylogขนาด14 9 ไบต์

{⟦ṗˢs+?}ᶜ

ลองออนไลน์!
กรณีทดสอบหลายรายการ

(-5 ไบต์ทั้งหมดขอบคุณ @Kroppeb!)

คำอธิบาย:

{⟦ṗˢs+?}ᶜ
{      }ᶜ     Count the number of ways this predicate can succeed:
 ⟦            Range from 0 to input
  ṗˢ          Select only the prime numbers
    s         The list of prime numbers has a substring (contiguous subset)
     +        Whose sum
      ?       Is the input

คุณสามารถกอล์ฟมันโดยการคำนวณ⟦ṗˢภายในวง ฉันได้รับ{⟦ṗˢs+;?=}ᶜชุดทดสอบนี้: ลองออนไลน์!
Kroppeb

รู้ว่าผมสามารถแทนที่;?=โดย?และได้รับ{⟦ṗˢs+?}ᶜ(9 bytes)
Kroppeb

@Kroppeb แน่นอน! นั่นเป็นคำตอบที่สง่างามกว่ามาก ขอขอบคุณ.
sundar - Reinstate Monica

3

เรติน่า 0.8.2 , 68 ไบต์

.+
$*_$&$*
_
$`__¶
A`^(__+)\1+$
m)&`^((_)|¶)+¶[_¶]*(?<-2>1)+$(?(2)1)

ลองออนไลน์! ลิงค์นั้นรวมถึงกรณีทดสอบที่เร็วกว่า คำอธิบาย:

m)

รันสคริปต์ทั้งหมดในโหมดหลายบรรทัดที่^และ$จับคู่กับทุกบรรทัด

.+
$*_$&$*

แปลงเป็นสองครั้งครั้งแรกโดยใช้_s แล้วใช้1s

_
$`__¶

_2n+1

A`^(__+)\1+$

ลบหมายเลขคอมโพสิตทั้งหมดในช่วง

&`^((_)|¶)+¶[_¶]*(?<-2>1)+$(?(2)1)

__1n


3

Husk , 9 8 ไบต์

-1 ไบต์ขอบคุณMr.Xcoder (ใช้อาร์กิวเมนต์ที่กำหนดชื่อ¹แทนS)!

#¹ṁ∫ṫ↑İp

ลองออนไลน์!

คำอธิบาย

#¹ṁ∫ṫ↑İp  -- example input: 3
#¹        -- count the occurrences of 3 in
      İp  -- | primes: [2,3,5,7..]
     ↑    -- | take 3: [2,3,5]
    ṫ     -- | tails: [[2,3,5],[3,5],[5]]
  ṁ       -- | map and flatten
   ∫      -- | | cumulative sums
          -- | : [2,5,10,3,8,5]
          -- : 1

เป็นโปรแกรมแบบเต็ม#¹ṁ∫ṫ↑İpควรบันทึก 1 ไบต์
Mr. Xcoder



2

ทำความสะอาด , 100 98 ไบต์

import StdEnv,StdLib
$n=sum[1\\z<-inits[i\\i<-[2..n]|all(\j=i/j*j<i)[2..i-1]],s<-tails z|sum s==n]

ลองออนไลน์!

กำหนดฟังก์ชั่น$ :: Int -> Intที่ทำงานตามที่อธิบายไว้ด้านล่าง:

$ n                              // the function $ of n is
    = sum [                      // the sum of
        1                        // 1, for every 
        \\ z <- inits [          // prefix z of 
            i                    // i, for every
            \\ i <- [2..n]       // integer i between 2 and n
            | and [              // where every
                i/j*j < i        // j does not divide i
                \\ j <- [2..i-1] // for every j between 2 and i-1
            ]
        ]
        , s <- tails z           // ... and suffix s of the prefix z
        | sum s == n             // where the sum of the suffix is equal to n
    ]

(คำอธิบายสำหรับรุ่นที่เก่ากว่า แต่มีเหตุผลเหมือนกัน)


1
คำยกย่องพิเศษสำหรับการรับผลผลิตสำหรับ 34421.
ngm

2

Perl 6 , 53 ไบต์

{+grep $_,map {|[\+] $_},[\R,] grep *.is-prime,2..$_}

ลองออนไลน์!

ใช้ตัวดำเนินการลดรูปสามเหลี่ยมสองครั้ง กรณีทดสอบสุดท้ายช้าเกินไปสำหรับ TIO

คำอธิบาย

{                                                   } # Anonymous block
                               grep *.is-prime,2..$_  # List of primes up to n
                         [\R,]  # All sublists (2) (3 2) (5 3 2) (7 5 3 2) ...
          map {|[\+] $_},  # Partial sums for each, flattened
 +grep $_,  # Count number of occurrences

2

Japt, 17 ไบต์

จะต้องมีวิธีที่สั้นกว่านี้!

เล่นลูกเต๋าชนิดหนึ่งในกรณีทดสอบล่าสุด

õ fj x@ZãYÄ x@¶Xx

ลองใช้หรือเรียกใช้กรณีทดสอบทั้งหมด


คำอธิบาย

                      :Implicit input of integer U
õ                     :Range [1,U]
  fj                  :Filter primes
      @               :Map each integer at 0-based index Y in array Z
         YÄ           :  Y+1
       Zã             :  Subsections of Z of that length
             @        :  Map each array X
               Xx     :    Reduce by addition
              ¶       :    Check for equality with U
            x         :  Reduce by addition
     x                :Reduce by addition

2

Java 10, 195 194 184 182 ไบต์

n->{var L=new java.util.Stack();int i=1,k,x,s,r=0;for(;i++<n;){for(k=1;i%++k>0;);if(k==i)L.add(i);}for(x=L.size(),i=0;i<x;)for(k=i++,s=0;k<x;r+=s==n?1:0)s+=(int)L.get(k++);return r;}

-1 ไบต์ขอบคุณที่@ceilingcat
-10 ไบต์ขอบคุณที่@SaraJ

ลองออนไลน์

คำอธิบาย:

n->{                // Method with integer as both parameter and return-type
  var L=new java.util.Stack();
                    //  List of primes, starting empty
  int i=1,k,x,s,    //  Temp integers
      r=0;          //  Result-counter, starting at 0
  for(;i++<n;){     //  Loop `i` in the range [2, `n`]
    for(k=1;        //   Set `k` to 1
        i%++k>0;);  //   Inner loop which increases `k` by 1 before every iteration,
                    //   and continues as long as `i` is not divisible by `k`
    if(k==i)        //   If `k` is now still the same as `i`; a.k.a. if `i` is a prime:
      L.add(i);}    //    Add the prime to the List
  for(x=L.size(),   //  Get the amount of primes in the List
      i=0;i<x;)     //  Loop `i` in the range [0, amount_of_primes)
    for(s=0,        //   (Re)set the sum to 0
        k=i++;k<x;  //   Inner loop `k` in the range [`i`, amount_of_primes)
        r+=s==n?    //     After every iteration, if the sum is equal to the input:
            1       //      Increase the result-counter by 1
           :        //     Else:
            0)      //      Leave the result-counter the same by adding 0
      s+=(int)L.get(k++);
                    //    Add the next prime (at index `k`) to the sum
  return r;}        //  And finally return the result-counter

มันคล้ายกับJellyหรือ05AB1Eแค่ 190 ไบต์ XD
นี่เป็นการเปรียบเทียบสำหรับแต่ละส่วนที่เพิ่มเข้ามาเพื่อความสนุกสนาน

  1. ใช้การป้อนข้อมูล: (Jelly: 0 bytes) โดยปริยาย ; (05AB1E: 0 ไบต์) โดยปริยาย ; (Java 10: 5 ไบต์)n->{}
  2. สร้างรายการช่วงเวลาในช่วง[2, n]: (Jelly: 2 ไบต์) ÆR; (05AB1E: 2 ไบต์)ÅP ; (Java 10: 95 ไบต์)var L=new java.util.Stack();int i=1,k,x,s,r=0;for(;i++<n;){for(k=1;i%++k>0;);if(k==i)L.add(i);}
  3. รับรายการย่อยทั้งหมดอย่างต่อเนื่อง: (Jelly: 1 ไบต์) ; (05AB1E: 1 ไบต์) Œ; (Java 10: 55 ไบต์)for(x=L.size(),i=0;i<x;)for(k=i++;k<x;)และ(int)L.get(k++);
  4. รวมแต่ละรายการย่อย: (Jelly: 1 ไบต์) §; (05AB1E: 1 ไบต์) O; (Java 10: 9 ไบต์) ,sและ,s=0และs+=
  5. นับจำนวนที่เท่ากับอินพุต: (Jelly: 1 byte) ċ ; (05AB1E: 2 ไบต์) QO; (Java 10: 15 ไบต์) ,r=0และr+=s==n?1:0
  6. การส่งออกผล: (Jelly: 0 bytes) โดยปริยาย ; (05AB1E: 0 bytes) โดยปริยาย ; (Java 10: 9 ไบต์)return r;

1
คำยกย่องพิเศษสำหรับการรับผลผลิตสำหรับ 34421.
ngm

@ngm :) Java อาจไม่ดีในหลาย ๆ สิ่ง แต่ประสิทธิภาพที่ฉลาดมันค่อนข้างดี
Kevin Cruijssen

1
แม้แต่ทำงานใน 218918 หมดเวลากับ 3634531
ngm

1
@ngm ฉันประหลาดใจจริง ๆ ที่ยังเร็วพอที่จะทำ218918ใน 12.5 วินาทีต่อชั่วโมงพิจารณามันจะทำ218918-2 = 218,916ซ้ำภายในวงภายในของ: nการทำซ้ำสำหรับทุกนายก; 1 ซ้ำทุกเลขคู่ และอยู่ระหว่าง[2,p/2)การวนซ้ำสำหรับทุกเลขคี่ (ใกล้กับการวนซ้ำสองพันล้านครั้ง) หลังจากนั้นเพิ่ม19518จำนวนเฉพาะลงในรายการในหน่วยความจำ แล้วมันจะห่วงเพิ่มอีกsum([0,19518]) = 190,485,921ครั้งในวงซ้อนกันสอง .. 2223570640 ซ้ำทั้งหมดเป็นที่แน่นอน
Kevin Cruijssen

@ceilingcat ขอบคุณ รับสามารถที่จะเล่นกอล์ฟ 12 ไบต์มากขึ้นด้วย@SaraJ 's ตรวจสอบที่สำคัญทางเลือกลบต่อท้าย%iเนื่องจากเราตรวจสอบอยู่ในช่วงดังนั้นฉันจะไม่จำเป็นต้องตรวจสอบ[2, n] i=1:)
Kevin Cruijssen

1

Physica , 41 ไบต์

->x:Count[Sum@Sublists[PrimeQ$$[…x]];x]

ลองออนไลน์!

มันทำงานอย่างไร

->x:Count[Sum@Sublists[PrimeQ$$[…x]];x] // Full program.
->x:            // Define an anonymous function with parameter x.
    […x]        // Range [0 ... x] (inclusive).
        $$      // Filter-keep those that...
  PrimeQ        // Are prime.
 Sublists[...]  // Get all their sublists.
Sum@            // Then sum each sublist.
Count[...;x]    // Count the number of times x occurs in the result.

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.